WCF Data Services (oData): Dependency Injection with DataService
-
27-09-2019 - |
Question
How can I use my IoC container in a DataService
and the CurrentDataSource
inside for WCF Data Services? I have several services I want to pass into it. The "host" application or project for the DataService
is based on ASP.NET MVC 2.0. My IoC container is setup in the MvcApplication
.
Solution
This is not as "clean" as integrating your IoC container deep into the WCF factory classes but if you want to supply a DataSource you could use the Common Service Locator (link at bottom) with your existing IoC container and override the CreateDataSource method of DataService
protected override MyEntityContext CreateDataSource()
{
return ServiceLocator.Current.GetInstance<MyEntityContext>();
}
Common Service Locator library
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow