[英]WCF Data Service vs. WCF Service Library
我注意到我无法将WCF数据服务(.svc文件)添加到Visual Studio 2010中的WCF服务库项目。我对WCF有点陌生,并想知道我应该如何/为什么要将我的WCF数据服务放入其中自己组装。
Yes, you can host a WCF Data Service in your own assembly - with a few little tricks. 是的,您可以在自己的程序集中托管WCF数据服务 - 只需一些小技巧。 Doing so makes your solution cleaner - it separates the various pieces into more manageable bits, so I would definitely recommend doing this.
这样做可以使您的解决方案更清洁 - 它将各个部分分成更易于管理的部分,因此我绝对建议您这样做。
Here's how: 这是如何做:
put your data model (EF Data Model) into its own assembly, let's call it DataModel
将您的数据模型(EF Data Model)放入自己的程序集中,我们将其称为
DataModel
create a new class library project (call it MyDataServiceHost
) 创建一个新的类库项目(称之为
MyDataServiceHost
)
add a few references: 添加一些参考:
DataModel
assembly with the data layer DataModel
程序集与数据层 System.ServiceModel
System.ServiceModel.Web
System.Data.Services.Client
System.Data.Services
- you cannot pick this from the usual Add Reference
dialog under the .NET category - you need to browse for the assembly file. System.Data.Services
- 您无法从.NET类别下的常用“ Add Reference
对话框中选择此项 - 您需要浏览汇编文件。 Find the directory C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.0
(or C:\\Program Files (x86)\\...
on a 64-bit machine) and pick the System.Data.Services.dll
inside it C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.0
(或C:\\Program Files (x86)\\...
)并选择System.Data.Services.dll
里面的System.Data.Services.dll
add a new class to that class library and call it eg YourDataService.cs
- it will look something like this: 在该类库中添加一个新类并调用它,例如
YourDataService.cs
- 它看起来像这样:
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; } } }
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; 您可以将类命名为任何名称,并且必须从
DataService<T>
派生,其中T
是数据模型的名称; 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 如果您正在使用Entity Framework,那么它就是您的对象上下文类的名称 - 通常类似于
(database)Entities
或您在创建EDM时选择的任何内容
add another class to your new project, call it MyDataServiceHost.cs
and it will look something like this: 在你的新项目中添加另一个类,称之为
MyDataServiceHost.cs
,它看起来像这样:
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(); } } } }
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. 它实例化一个DataServiceHost,它派生自WebServiceHost(后者又派生自ServiceHost),它将为您启动WCF数据服务运行时。
now you can start up your WCF Data Service from any app using: 现在,您可以使用以下命令从任何应用启动WCF数据服务:
MyDataServiceHost.LaunchDataService("http://localhost:4444/YourService");
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数据服务的应用程序必须在其
app.config
(或web.config
)中具有连接字符串(EDM连接字符串,如果您使用的是Entity Framework)上班!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.