开发者

How do I use autofac to resolve a WCF Data Service?

开发者 https://www.devze.com 2023-02-12 17:14 出处:网络
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

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号