如何实现自托管WCF数据服务(http://localhost:1234/myDataService.svc/...)
我有一个项目需要实现 WCF 数据服务 (OData) 以从控制系统(.NET Framework 应用程序)检索数据。 WCF 数据服务需要由 .NET 应用程序托管(无 ASP.NET 和无 IIS)。
我最近看到了很多WCF数据服务的例子;它们都由 ASP.NET 应用程序托管。我还看到了自托管(控制台应用程序)示例,但它适用于 WCF 服务(而不是 WCF 数据服务)。
可以使用独立的 .NET 应用程序来托管 WCF 数据服务 (http://localhost:1234/mydataservice.svc/...)。
如果是,有人可以举个例子吗?
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).
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).
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚尝试了同样的事情 - 是的,您可以在自己的程序集中托管 WCF 数据服务 - 使用一些小技巧。
操作方法如下:
将数据模型(EF 数据模型)放入其自己的程序集中,我们称之为
DataModel
创建一个新的类库项目(称之为
MyDataServiceHost
)添加一些参考文献:
DataModel
程序集System.ServiceModel
System.ServiceModel.Web
System.Data.Services.Client
System.Data.Services
- 您无法从 .NET 类别下的常见Add Reference
对话框中选择它 - 您需要浏览程序集文件。找到目录C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
(或C:\Program Files (x86)\...
在 64 位计算机上)并选择其中的System.Data.Services.dll
向该类库添加一个新类并调用它,例如
YourDataService.cs
- 它看起来像这样:您可以为该类命名任何您喜欢的名称,并且它必须从
DataService
派生,其中T
是您的数据模型的名称;如果您使用实体框架,它是对象上下文类的名称 - 通常类似于(database)Entities
或您在创建 EDM 时选择的任何内容向新项目添加另一个类,将其命名为
MyDataServiceHost.cs
,它将如下所示:它实例化一个 DataServiceHost,它派生自 WebServiceHost(WebServiceHost 又派生自 ServiceHost),并且它将为您启动 WCF 数据服务运行时。
现在您可以使用以下方式从任何应用程序启动 WCF 数据服务:
最后要记住的是:用于启动 WCF 数据服务的应用程序必须具有其 app.config(或 web.config)中的连接字符串(EDM 连接字符串,如果您使用实体框架)才能正常工作!
I just tried the same thing - and yes, you can host a WCF Data Service in your own assembly - with a few little tricks.
Here's how:
put your data model (EF Data Model) into its own assembly, let's call it
DataModel
create a new class library project (call it
MyDataServiceHost
)add a few references:
DataModel
assembly with the data layerSystem.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 Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
(orC:\Program Files (x86)\...
on a 64-bit machine) and pick theSystem.Data.Services.dll
inside itadd a new class to that class library and call it e.g.
YourDataService.cs
- it will look something like this:You can name the class anything you like, and it has to derive from
DataService<T>
whereT
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 EDMadd another class to your new project, call it
MyDataServiceHost.cs
and it will look something like this: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.
now you can start up your WCF Data Service from any app using:
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!