Create a WCF Data Service for Use in a Service Host

I have a service application that hosts multiple WCF endpoints using different bindings. Now I want to host the WCF data service in the same service node. This post makes me believe that this is possible. However, each individual example of creating a WCF data service creates it from an ASP.NET web project, not a class library or service application. In fact, the WCF data service does not even appear in Add New Item if one of these project types is selected.
My question is how to create a WCF data service inside a class library that will be called by a service node that already contains several other WCF endpoints? The link that I already referred to shows me how to host the data service after creating it, but since it does not appear as an option to add a new item, m kinna is stuck.

+4
source share
3 answers

Yes, you can host the WCF data service in your own assembly - with a few small tricks.

Here's how:

  • put your data model (EF Data Model) in your own assembly, call it DataModel

  • create a new class library project (name it MyDataServiceHost )

  • add some links:

    • DataModel assembly with data layer
    • System.ServiceModel
    • System.ServiceModel.Web
    • System.Data.Services.Client
    • System.Data.Services - you cannot select this from the usual Add Reference dialog in the .NET category - you need to view the assembly file. Locate the C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 (or C:\Program Files (x86)\... directory on a 64-bit machine) and select System.Data.Services.dll inside him
  • add a new class to this class library and call it, for example. 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 that you like, and it should be obtained from the DataService<T> , where T is the name of your data model; if you use the Entity Framework, this is the name of your object context class - usually something like (database)Entities or what you selected when creating the EDM

  • add another class to your new project, name 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 creates an instance of DataServiceHost, which is derived from WebServiceHost (which, in turn, is derived from ServiceHost), and it will start the WCF service runtime for you.

  • Now you can start your WCF data service from any application using:

     MyDataServiceHost.LaunchDataService("http://localhost:4444/YourService"); 
  • The last thing to remember: the application that you use to start the WCF data service must have a connection string (EDM connection string if you use the Entity Framework) in your app.config (or web.config) for this to work!

+7
source

One way you can do this is to create an ASP.NET project to host the WCF data service, and then define the user data service host (scroll down to the "Define User Data Node" section), which acts as an intermediary between your existing host WCF and data service.

0
source

After much digging, I found the answer to 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 in my ObjectContext , which contains all my objects. Works great

0
source

Source: https://habr.com/ru/post/901398/


All Articles