WCF数据服务 vs WCF服务库

6

我发现在Visual Studio 2010的WCF服务库项目中无法添加WCF数据服务(.svc文件)。作为一个WCF新手,我想知道为什么/如何将我的WCF数据服务放入自己的程序集中。


3
WCF 数据服务只能添加到 Web 应用程序(可能还包括“网站”)项目中 - 至少在使用 Visual Studio 模板时是如此。 - marc_s
1个回答

3

是的,你可以将WCF数据服务托管在你自己的程序集中——只需一些小技巧即可实现。这样做可以使你的解决方案更加清晰——它将各个部分分离成更易管理的组件,因此我强烈建议这样做。

下面是具体步骤:

  • 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:

    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!


是的,我在之前的帖子中看到过这个问题:https://dev59.com/qlPTa4cB1Zd3GeqPk6Cw。这会创建svc文件吗? - Ryan
@Ryan:不需要*.svc文件,因为你自己创建了DataServiceHost并在代码中定义了服务可访问的基本地址-没有必要有一个*.svc文件。 - marc_s
谢谢提供信息!我不确定这是否是我想要的方式。 - Ryan
@marc_c 实际上,这样做和创建一个WCF服务应用程序有什么区别? - Ryan
啊,我明白了,这很好知道。如果我需要将这个WCF数据服务用于iPhone客户端或其他非微软平台,是否仍然可以启动它?还是说这就是我主要考虑使用WCF服务应用程序和其中的WCF数据服务的原因? - Ryan
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接