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 layerSystem.ServiceModelSystem.ServiceModel.WebSystem.Data.Services.ClientSystem.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> {
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!
source share