如何使用autofac解析WCF数据服务?

时间:2011-02-23 13:46:50

标签: asp.net-mvc wcf-data-services autofac

我正在使用WCF Data Services Toolkit及其白皮书Building OData Services on Top of Existing APIs创建WCF数据服务。该服务正被添加到已配置为使用AutofacDependencyResolver的现有MVC 3网站。白皮书中提供的用于公开服务的代码是:

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);
  ...
}

有没有一种简单的方法来修改它以通过Autofac解析服务,还是需要实现我自己的ServiceHostFactory?

1 个答案:

答案 0 :(得分:2)

我想我明白了:

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);
  ...
}

其他地方不要忘记设置

AutofacWebServiceHostFactory.Container = container;