Today I am digging into WCF Data Service and I have a question regarding this. Can I create WCF Data service as Library and just only create WCF data Service in our existing web app and take reference that library using Factory property so service will deploy with existing web application.
As I know We can create WCF Service Library and only need to take reference that library in Web application like :
- Create a WCF Library and implement service contract
-
Create a Web application and add new item as Wcf service file then take reference WCF library
<%@ ServiceHost Service=”MyServiceLibrary.MyService” Factory=”System.ServiceModel.Activation.WebServiceHostFactory” />
Instead of a service library, I want to create OData service library.
Thanks
Yes, you can host a WCF Data Service in your own assembly – with a few little tricks. I researched this a while ago and came up with these steps / instructions.
Here’s how:
put your data model (EF Data Model) into its own assembly, let’s call it
DataModelcreate a new class library project (call it
MyDataServiceHost)add a few references:
DataModelassembly with the data layerSystem.ServiceModelSystem.ServiceModel.WebSystem.Data.Services.ClientSystem.Data.Services– you cannot pick this from the usualAdd Referencedialog under the .NET category – you need to browse for the assembly file. Find the directoryC:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0(orC:\Program Files (x86)\...on a 64-bit machine) and pick theSystem.Data.Services.dllinside itadd a new class to that class library and call it e.g.
YourDataService.cs– it will look something like this:You can name the class anything you like, and it has to derive from
DataService<T>whereTis 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)Entitiesor whatever you picked when you created the EDMadd another class to your new project, call it
MyDataServiceHost.csand it will look something like this: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:
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!