Does the simple MVC 4 injector support ASP.NET Web API?

I am new to the IOC Simple Injector container. I will start working on a project that will require a multi-tenant implementation of ASP.NET MVC using the ASP.NET MVC 4 Web API.

My question is: does the simple MVC 4 injector support ASP.NET Web API? Reading simple injector documentation such as this> makes references to MVC 3, and I would like to know if MVC 4 is also supported.

+17
c # ioc-container simple-injector
Jun 28 '12 at 23:28
source share
2 answers

Does the simple IOC MVC 4 injector support ASP.NET Web API?

It does not currently support the MVC4 web API, but support will be added in the future. The integration guide will be updated when this happens.




UPDATE : Web API support added in Simple Injector 2.5.




In the meantime, you can create your own implementation of System.Web.Http.Dependencies.IDependencyResolver for a simple injector. The following is an implementation for working with the web API in the IIS hosting environment:

 public class SimpleInjectorHttpDependencyResolver : System.Web.Http.Dependencies.IDependencyResolver { private readonly Container container; public SimpleInjectorHttpDependencyResolver( Container container) { this.container = container; } public System.Web.Http.Dependencies.IDependencyScope BeginScope() { return this; } public object GetService(Type serviceType) { IServiceProvider provider = this.container; return provider.GetService(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { IServiceProvider provider = this.container; Type collectionType = typeof(IEnumerable<>).MakeGenericType(serviceType); var services =(IEnumerable<object>)this.ServiceProvider.GetService(collectionType); return services ?? Enumerable.Empty<object>(); } public void Dispose() { } } 

This implementation does not use scope, since you need to use the Per Web Api Request to implement the scope within the web host (where the request may end in a different thread than where it was launched).

Due to the way the Web API is designed , it is very important to explicitly register all web API controllers. You can do this using the following code:

 var services = GlobalConfiguration.Configuration.Services; var controllerTypes = services.GetHttpControllerTypeResolver() .GetControllerTypes(services.GetAssembliesResolver()); foreach (var controllerType in controllerTypes) { container.Register(controllerType); } 

You can register SimpleInjectorHttpDependencyResolver as follows:

 // NOTE: Do this as last step, after registering the controllers. GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorHttpDependencyResolver(container); 
+24
Jun 29 2018-12-12T00: 00Z
source share

Short answer: yes, it works. Below is a snippet of code that works for my project using both the Web API and MVC4 (partly based on Stephen's answer above):

 var container = new Container(); //Register you types in container here container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); container.RegisterMvcAttributeFilterProvider(); var controllerTypes = from type in Assembly.GetExecutingAssembly().GetExportedTypes() where typeof(ApiController).IsAssignableFrom(type) where !type.IsAbstract where !type.IsGenericTypeDefinition where type.Name.EndsWith("Controller", StringComparison.Ordinal) select type; foreach (var controllerType in controllerTypes) { container.Register(controllerType); } container.Verify(); DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); 

To access your types in API controllers, you can use the following code snippet:

 DependencyResolver.Current.GetService<InterfaceName>() 
+3
May 13 '13 at 18:41
source share



All Articles