How do I use autofac to resolve a WCF Data Service?
I am creating a WCF Data Service using the WCF Data Services Toolkit and its whitepaper Building OData Services on Top of Existing APIs. The service is being added to an existing MV开发者_JAVA技巧C 3 website that is already configured to use the AutofacDependencyResolver. The code provided in the whitepaper for exposing the service is:
protected void Application_Start()
{
var factory = new DataServiceHostFactory();
var serviceRoute = new ServiceRoute("odata", factory, typeof(CrmODataService));
serviceRoute.Defaults = new RouteValueDictionary { { "serviceType", "odata" } };
serviceRoute.Constraints = new RouteValueDictionary { { "serviceType", "odata" } };
RouteTable.Routes.Add("odata", serviceRoute);
...
}
Is there a simple way to modify this to resolve the service via Autofac or do I need to implement my own ServiceHostFactory?
I think I figured it out:
protected void Application_Start()
{
var factory = new AutofacWebServiceHostFactory();
var serviceRoute = new ServiceRoute("odata", factory, typeof(CrmODataService));
serviceRoute.Defaults = new RouteValueDictionary { { "serviceType", "odata" } };
serviceRoute.Constraints = new RouteValueDictionary { { "serviceType", "odata" } };
RouteTable.Routes.Add("odata", serviceRoute);
...
}
And elsewhere don't forget to set
AutofacWebServiceHostFactory.Container = container;
精彩评论