创建WCF数据服务以在服务主机中使用

发布于 2024-10-13 16:04:54 字数 496 浏览 6 评论 0原文

我有一个服务应用程序,通过不同的绑定托管多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱的十字路口 2024-10-20 16:04:54

是的,您可以在自己的程序集中托管 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 - 它看起来像这样:

    使用 System.Data.Services;
    使用 System.Data.Services.Common;
    
    使用数据模型;
    
    命名空间 MyDataServiceHost
    {
        公共类 YourDataService : DataService;
        {
            // 该方法仅调用一次来初始化服务范围的策略。
            公共静态无效InitializeService(DataServiceConfiguration配置)
            {
                // TODO: 设置规则来指示哪些实体集和服务操作是可见的、可更新的等。
                // 示例:
                config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            }
        }
    }
    

    您可以为该类命名任何您喜欢的名称,并且它必须从 DataService 派生,其中 T 是您的数据模型的名称;如果您使用实体框架,它是对象上下文类的名称 - 通常类似于 (database)Entities 或您在创建 EDM 时选择的任何内容

  • 向新项目添加另一个类,将其命名为 MyDataServiceHost.cs,它将如下所示:

    使用系统;
    使用系统.数据.服务;
    
    使用数据模型;
    
    命名空间 MyDataServiceHost
    {
        公共类 MyDataServiceHost
        {
            公共静态无效LaunchDataService(字符串基地址)
            {
                Uri[] baseAddresses = 新 Uri[1];
                baseAddresses[0] = new Uri(baseAddress);
    
                使用(DataServiceHost主机=新DataServiceHost(typeof(YourDataService),baseAddresses))
                {
                    主机.Open();
                    Console.WriteLine("DataService 已启动并运行......");
    
                    Console.ReadLine();
                    主机.关闭();
                }
            }
        }
    }
    

    它实例化一个 DataServiceHost,它派生自 WebServiceHost(WebServiceHost 又派生自 ServiceHost),并且它将为您启动 WCF 数据服务运行时。

  • 现在您可以使用以下方式从任何应用程序启动 WCF 数据服务:

    MyDataServiceHost.LaunchDataService("http://localhost:4444/YourService");
    
  • 最后要记住的是:用于启动 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:

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

如此安好 2024-10-20 16:04:54

实现此目的的一种方法是创建一个 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.

叹倦 2024-10-20 16:04:54

经过大量挖掘,我在这篇文章中找到了答案: 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 my ObjectContext that holds all my Entities. Worked perfectly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文