问题描述
我有一个项目需要实现 WCF 数据服务 (OData) 以从控制系统(.NET Framework 应用程序)检索数据.WCF 数据服务需要由 .NET 应用程序托管(无 ASP.NET 和无 IIS).
I have a project that needs to implement WCF data services (OData) to retrieve data from a control system (.NET Framework Application). The WCF data service needs to be hosted by the .NET application (No ASP.NET and NO IIS).
最近看到很多WCF数据服务的例子;它们都由 ASP.NET 应用程序托管.我还看到了自托管(控制台应用程序)示例,但它适用于 WCF 服务(不是 WCF 数据服务).
I have seen many WCF Data Service examples recently; they are all hosted by ASP.NET application. I also see the self-host (console application) examples, but it is for WCF Service (not WCF Data Service).
可以使用独立的 .NET 应用程序来托管 WCF 数据服务 (http://localhost:1234/mydataservice.svc/...).
It is possible to have a standalone .NET Applications to host WCF Data Services (http: //localhost:1234/mydataservice.svc/...).
如果是,谁能提供一个例子?
If yes, can someone provide an example?
推荐答案
我刚刚尝试了同样的事情 - 是的,您可以在自己的程序集中托管 WCF 数据服务 - 只需一些小技巧.
I just tried the same thing - and yes, you can host a WCF Data Service in your own assembly - with a few little tricks.
方法如下:
把你的数据模型(EF Data Model)放到它自己的程序集中,我们称之为
DataModel
创建一个新的类库项目(称之为MyDataServiceHost
)
create a new class library project (call it MyDataServiceHost
)
添加一些参考:
- 您的
DataModel
与数据层的组合 System.ServiceModel
System.ServiceModel.Web
System.Data.Services.Client
System.Data.Services
- 您不能从 .NET 类别下通常的Add Reference
对话框中选择它 - 您需要浏览程序集文件.找到目录C:Program FilesReference AssembliesMicrosoftFramework.NETFrameworkv4.0
(或C:Program Files (x86)...
在 64 位机器上)并选择其中的System.Data.Services.dll
- your
DataModel
assembly with the data layer System.ServiceModel
System.ServiceModel.Web
System.Data.Services.Client
System.Data.Services
- you cannot pick this from the usualAdd Reference
dialog under the .NET category - you need to browse for the assembly file. Find the directoryC:Program FilesReference AssembliesMicrosoftFramework.NETFrameworkv4.0
(orC:Program Files (x86)...
on a 64-bit machine) and pick theSystem.Data.Services.dll
inside it
向该类库添加一个新类并调用它,例如YourDataService.cs
- 它看起来像这样:
add a new class to that class library and call it e.g. YourDataService.cs
- it will look something like this:
using System.Data.Services;
using System.Data.Services.Common;
using DataModel;
namespace MyDataServiceHost
{
public class YourDataService : DataService<YourModelEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
}
您可以随意命名该类,它必须从 DataService
派生,其中 T
是您的数据模型的名称;如果您使用的是实体框架,则它是您的对象上下文类的名称 - 通常类似于 (database)Entities
或您在创建 EDM 时选择的任何内容
You can name the class anything you like, and it has to derive from DataService<T>
where T
is the name of your data model; if you're using Entity Framework, it's the name of your object context class - typically something like (database)Entities
or whatever you picked when you created the EDM
向您的新项目添加另一个类,将其命名为 MyDataServiceHost.cs
,它看起来像这样:
add another class to your new project, call it MyDataServiceHost.cs
and it will look something like this:
using System;
using System.Data.Services;
using DataModel;
namespace MyDataServiceHost
{
public class MyDataServiceHost
{
public static void LaunchDataService(string baseAddress)
{
Uri[] baseAddresses = new Uri[1];
baseAddresses[0] = new Uri(baseAddress);
using(DataServiceHost host = new DataServiceHost(typeof(YourDataService), baseAddresses))
{
host.Open();
Console.WriteLine("DataService up and running.....");
Console.ReadLine();
host.Close();
}
}
}
}
它实例化一个 DataServiceHost,它派生自 WebServiceHost(后者又派生自 ServiceHost),它会为您启动 WCF 数据服务运行时.
It instantiates a DataServiceHost, which is derived from WebServiceHost (which in turn is derived from ServiceHost) and it will spin up the WCF Data Service runtime for you.
现在您可以使用以下任何应用程序启动您的 WCF 数据服务:
now you can start up your WCF Data Service from any app using:
MyDataServiceHost.LaunchDataService("http://localhost:4444/YourService");
要记住的最后一件事:用于启动 WCF 数据服务的应用必须在其app.config(或 web.config)为了让它工作!
last thing to remember: the app that you use to launch the WCF Data Service must have the connection string (the EDM connection string, if you're using Entity Framework) in its app.config (or web.config) in order for this to work!
这篇关于如何实现自托管 WCF 数据服务(http://localhost:1234/myDataService.svc/...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!