Does Simple Injector supports MVC 4 ASP.NET Web AP

2020-02-10 03:11发布

问题:

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.

回答1:

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


回答2:

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>()