Skip to content

Self Hosting ADO.Net Data Services (Astoria)


As part of the recent Surface application there was a requirement to host both a “normal” WCF service, and an ADO.Net Data Services service inside the main application (rather than a service running under a website).  The former is very straightforward and well documented, but I was a bit short of information on the latter and the information I did have hadn’t been updated for the various RTM changes.

Luckily it’s very easy to do, so I thought I’d throw together a quick example.

Example Data

For this example I’ve used Entity Framework and the Northwind database. It’s dependant on SQL Server Express running as the SQLEXPRESS instance on the local machine, but ADO.Net Data Services can expose all kinds of data, take a look at MSDN or your friendly neighbourhood search engine for more information :-) 


To get things going we need two particular namespaces:

Service Definition

To define our service we simply create a new class, inherited from DataService<T>, and implement a single method InitializeService to define what entities to expose, and what permissions to give.  In this example DemoEntites is the name I gave to our Entity Framwork Model and we will just expose everything with full access.  For more information on this method, take a look at the MSDN documentation.

namespace SelfHostingDemo
    public class SelfHostedService : DataService<DemoEntities>
        public static void InitializeService(IDataServiceConfiguration config)
            config.UseVerboseErrors = true;
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);

The second part of defining the service is in the app.config, so add one if you haven’t already got one, and we need to add a system.serviceModel section and add the definition for our service:

    <service name="SelfHostingDemo.SelfHostedService">
      <endpoint address="http://localhost:8751/selfhosted/" binding="webHttpBinding" contract="System.Data.Services.IRequestHandler" />

If you are running on Vista, and aren’t running with Administrator privileges, you will need to use netsh (as an Administrator) to allow us to bind to that endpoint address.  In this example we will just give the Users group access to listen on anything on port 8751:

netsh http add urlacl url=http://+:8751/ user=Users listen=yes

Firing It Up

To fire up our service we create a new WebServiceHost.  In this example I start it up inside Main():

    System.ServiceModel.Web.WebServiceHost aHost = new System.ServiceModel.Web.WebServiceHost(typeof(SelfHostedService));
catch (Exception ex)
    Trace.WriteLine(ex, "Failed to start CRUD host");

And that’s all we need to do.  If you launch the example application you will be presented with a single button that opens the address of our service in your browser.  From there you should be able to navigate round the various entities and relationships.

  • Twitter
  • DotNetKicks
  • DotNetShoutout
  • Google Bookmarks
  • Digg
  • Live
  • Technorati
  • StumbleUpon
  • email
  • Netvibes
  • Print
  • Reddit

Technorati Tags: , , ,

Posted in ADO.Net.

Tagged with , , , .