17

我是 Simple Injector IOC 容器的新手。我将开始在一个需要使用 MVC 4 ASP.NET Web API 实现多租户 ASP.NET MVC 的项目中工作。

我的问题是:Simple 注入器是否支持 MVC 4 ASP.NET Web API?阅读像这样的简单注入器文档会引用 MVC 3,我想知道是否也支持 MVC 4。

4

2 回答 2

24

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); 
于 2012-06-29T09:42:25.590 回答
3

简短的回答是:是的,它有效。下面是适用于我的项目的代码片段,它同时具有 Web API 和 MVC4 控制器(部分基于上面 Steven 的响应):

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

要在 API 控制器中访问您的类型,您可以使用以下代码片段:

DependencyResolver.Current.GetService<InterfaceName>()
于 2013-05-13T18:41:22.453 回答