简体   繁体   中英

Does Simple Injector supports MVC 4 ASP.NET Web API?

I am new to Simple Injector IOC container. I will start working in a project which will require a Multi-tenant ASP.NET MVC implementation using MVC 4 ASP.NET Web API.

My question is: Does Simple injector support MVC 4 ASP.NET Web API? Reading simple injector documentation like this makes references to MVC 3 and I would like to know if MVC 4 also is supported.

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

It has currently no support for MVC4 Web API, but support will be added in the future. The integration guide will be updated when this happens.


UPDATE : Web API support has been added to Simple Injector 2.5.


In the meantime, you can create your own System.Web.Http.Dependencies.IDependencyResolver implementation for the Simple Injector. Below is the implementation for working with Web API in a IIS hosted 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 implements no scoping, since you need to use the Per Web Api Request lifetime for implementing scoping inside a web hosted environment (where a request may end on a different thread than where it started).

Because of the way 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 the SimpleInjectorHttpDependencyResolver as follows:

// NOTE: Do this as last step, after registering the controllers.
GlobalConfiguration.Configuration.DependencyResolver = 
    new SimpleInjectorHttpDependencyResolver(container); 

The short answer is: yes, it works. Below is the code snippet that works for my project with both Web API and MVC4 controllers (partially based on Steven's response 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 the API controllers you could use the following code snippet:

DependencyResolver.Current.GetService<InterfaceName>()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM