2011-02-23 70 views
1

我正在使用WCF Data Services Toolkit及其白皮書Building OData Services on Top of Existing APIs創建WCF數據服務。該服務正被添加到已配置爲使用AutofacDependencyResolver的現有MVC 3網站中。在白皮書公開服務提供的代碼是:如何使用autofac解析WCF數據服務?

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?

回答

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;