Yes, you can host a WCF Data Service in your own assembly - with a few little tricks. I researched this a while ago and came up with these steps / instructions.
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:
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 usual Add Reference dialog under the .NET category - you need to browse for the assembly file. 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
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;
}
}
}
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
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();
}
}
}
}
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!
发布评论
评论(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 连接字符串,如果您使用的是实体框架)才能使其正常工作!
Yes, you can host a WCF Data Service in your own assembly - with a few little tricks. I researched this a while ago and came up with these steps / instructions.
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!