I have a project that needs to implement WCF data services (OData) to retrieve data from a control system (.NET Framework Application). The WCF data service needs to be hosted by the .NET application (No ASP.NET and NO IIS).
I have seen many WCF Data Service examples recently; they are all hosted by ASP.NET application. I also see the self-host (console application) examples, but it is for WCF Service (not WCF Data Service).
It is possible to have a standalone .NET Applications to host WCF Data Services (http: //localhost:1234/mydataservice.svc/...).
If yes, can someone provide an example?
I just tried the same thing - and 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:
DataModel
assembly with the data layerSystem.ServiceModel
System.ServiceModel.Web
System.Data.Services.Client
System.Data.Services
- you cannot pick this from the usualAdd Reference
dialog 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.dll
inside 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>
whereT
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 EDMadd another class to your new project, call it
MyDataServiceHost.cs
and 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!