创建WCF数据服务以在服务主机中使用
我有一个服务应用程序,通过不同的绑定托管多个 WCF 端点。我现在想在同一服务主机中托管 WCF 数据服务。 这篇文章让我相信这是可能的。然而,关于如何创建 WCF 数据服务的每个示例都是从 ASP.NET Web 项目内部创建的,而不是从类库或服务应用程序内部创建的。事实上,当从这些项目类型之一中选择时,WCF 数据服务甚至不会显示在“添加新项”中。
我的问题是如何在类库中创建 WCF 数据服务,该服务将由已托管多个其他 WCF 端点的服务主机调用? 我已经引用的链接向我展示了如何在之后托管数据服务它已创建,但由于它没有显示为“添加新项目”的选项,所以我陷入了困境。
I have a service application that host several WCF endpoints through different bindings. I want now to host a WCF Data Service in the same service Host. This post makes me believe it's possible. However, every single example on how to create a WCF Data Service creates it from inside an ASP.NET Web project, not a Class Library or Service Application. In fact, the WCF Data Service doesn't even show up in the Add New Item when selected from one of these project types.
My question is how do I create a WCF Data Service inside a Class Library, that will be called by a service host, that's already hosting several other WCF endpoints? The link I already referenced shows me how to host the Data Service after it's created, but since it doesn't show up as an option of the Add New Item, I'm kinna stuck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,您可以在自己的程序集中托管 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 连接字符串,如果您使用的是实体框架)才能使其正常工作!
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!
实现此目的的一种方法是创建一个 ASP.NET 项目来包含 WCF 数据服务,然后 定义自定义数据服务主机(向下滚动到“定义自定义数据服务主机”部分),充当现有 WCF 主机和数据服务之间的中介。
One way you may be able to accomplish this is to create an ASP.NET project to contain the WCF Data Service and then define a custom data service host (scroll down to the 'Defining a Custom Data Service Host' section) that acts as a mediator between your existing WCF host and the Data Service.
经过大量挖掘,我在这篇文章中找到了答案: http://social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/3191377e-f3f9-4d46-8daf-431cf74cef7c
我需要使用
DataService
类,传递保存所有实体的ObjectContext
。工作完美After a lot of digging, I found the answer at this post: http://social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/3191377e-f3f9-4d46-8daf-431cf74cef7c
I need to use the
DataService<T>
class, passing myObjectContext
that holds all my Entities. Worked perfectly