WCF Data Service and WCF Service Library

I noticed that I cannot add a WCF data service (.svc file) to the WCF Services Library project in Visual Studio 2010. I am a little new to WCF and wondered how / why I should put my WCF Service data in my assembly.

+6
source share
1 answer

Yes, you can host the WCF data service in your own assembly - with a few little tricks. This makes your solution cleaner - it divides the various parts into more manageable bits, so I definitely recommend doing this.

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!

+3
source

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


All Articles