Self hosting an ADO.NET data service
All the samples I’ve seen for ADO.NET Data Services show hosting the service in IIS. This is a natural choice, especially considering the rich hosting environment IIS provides and the tooling support provided by Visual Studio. However, it is not the only option for hosting ADO.NET Data Services. These services are just WCF services and therefore are capable of being hosted in any environment just like any other WCF service. In this post, I will show how you can host an ADO.NET Data Service in a Windows Service.
First, create the Windows Service project just like you normally would and add the service and process installers. If you’re not sure how to do this, follow the steps explained here. I named my Windows Service project ADOServiceHost.
In Solution Explorer, right-click the Windows Service project and select Add -> New Item. Add an ADO.NET Entity Data Model and follow the steps to generate the model from an existing database. I have the AdventureWorksLT database for SQL Server 2005 installed on my machine and I’m using that as the source for my model, which I named AWDataModel. You should now be able to explore the Entity Data Model by viewing the .edmx file that was created (see below).
In Solution Explorer, right-click the References folder in your Windows Service project and select Add Reference. Add references to the System.Data.Services.dll and the System.ServiceModel.Web.dll assemblies.
In Solution Explorer, right-click the Windows Service project and select Add –> New Item. Add a C# Class which will be the class that is your data service. I named mine AWDataService.cs. To make this an ADO.NET Data Service that exposes the Entity Data Model above, you have to derive from DataService<T> and you need to provide a static InitializeService method to specify things like entity access rules (see below).
The last piece of code needed is the code to host the AWDataService defined above. In your Windows Service project, open the code for the service that the Windows Service template created by right-clicking on Service1.cs and selecting View Code (see below).
At the top of this file you will need to add a using clause to bring in the System.Data.Services namespace. Then, you can new up an instance of DataServiceHost and open it in the OnStart method. Similarly, close the host in the OnStop method (see below).
Build the solution.
Open a Visual Studio.NET Command Prompt and navigate to the output folder for the Windows Service project. Using InstallUtil.exe, install the Windows Service (see below).
Next, start the service. You can do this from the service control panel services applet or from the command prompt. The name of your service will be the "Display Name” you assigned it when you added the service installer above. I named mine “AW .NET Data Service”. To start from a command prompt, just use the net start command (see below).
Now that the service is running, you should be able to start interacting with the resources from your model using your browser (see below).