The official ADO.NET Data Services (hm, am I the only who thinks that this - again - is not a perfect name...?) documentation doesn't alk about and doesn't show how to self-host your Astoria services - the samples and quickstarts always use ASP.NET/IIS to expose the WCF-based services that Astoria is using.
By looking a bit around in Reflector I managed to create a self-hosted Astoria service which (for this example) is hosted in a Console application.
We have several steps to fulfill our task:
- Create the ADO.NET Entity Model
- Implement your Astoria Service
- Configure endpoint(s)
- Create a ServiceHost instance
Astoria uses one generic universal contract for implementing the service base class. The service class you already built above is a derivate of WebDataService<T> and the WCF universal ServiceContract is Microsoft.Data.Web.IRequestHandler which can be found in the Microsoft.Data.Web assembly.
[ServiceContract]
public interface IRequestHandler
{
[OperationContract]
[WebInvoke(UriTemplate="*", Method="*")]
Message ProcessRequestForMessage(Stream messageBody);
}
So everything we need to do now is to create an endpoint definition which uses these artifacts and leverages the webHttpBinding which was introduced in .NET 3.5
<system.serviceModel>
<services>
<service name="SelfHostedAstoria.WebDataService">
<endpoint
address="http://localhost:7777/Services/Data/NW"
binding="webHttpBinding"
behaviorConfiguration="webHttp"
contract="Microsoft.Data.Web.IRequestHandler" />
</service>
</services>
</system.serviceModel>
In order to actually expose our Astoria Service we need to hook up a ServiceHost instance. In our case we will use the WebServiceHost (living in System.ServiceModel.Web) which already takes care about setting up the needed behavior on all webHttpBinding-based endpoints.
WebServiceHost host = new WebServiceHost(typeof(WebDataService));
host.Open();
...
Easy! Now we have a self-hosted Astoria service.
Comments