简体   繁体   English

Simple Injector是否支持MVC 4 ASP.NET Web API?

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

I am new to Simple Injector IOC container. 我是Simple Injector IOC容器的新手。 I will start working in a project which will require a Multi-tenant ASP.NET MVC implementation using MVC 4 ASP.NET Web API. 我将开始在一个项目中工作,该项目需要使用MVC 4 ASP.NET Web API的多租户ASP.NET MVC实现。

My question is: Does Simple injector support MVC 4 ASP.NET Web API? 我的问题是:Simple injector支持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. 喜欢读简单的注射器文档使得引用MVC 3,我想知道,如果MVC 4还支持。

Does Simple injector IOC support MVC 4 ASP.NET Web API? Simple injector IOC是否支持MVC 4 ASP.NET Web API?

It has currently no support for MVC4 Web API, but support will be added in the future. 它目前不支持MVC4 Web API,但将来会添加支持。 The integration guide will be updated when this happens. 发生这种情况时,将更新集成指南


UPDATE : Web API support has been added to Simple Injector 2.5. 更新 :Simple Injector 2.5中添加了Web API支持


In the meantime, you can create your own System.Web.Http.Dependencies.IDependencyResolver implementation for the Simple Injector. 在此期间,您可以为Simple Injector创建自己的System.Web.Http.Dependencies.IDependencyResolver实现。 Below is the implementation for working with Web API in a IIS hosted environment: 以下是在IIS托管环境中使用Web API的实现:

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). 此实现不实现任何作用域,因为您需要使用Per Web Api请求生存期来在Web托管环境中实现作用域(其中请求可能在与其开始的不同的线程上结束)。

Because of the way Web API is designed , it is very important to explicitly register all Web API Controllers. 由于Web API的设计方式 ,显式注册所有Web API控制器非常重要。 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: 您可以按如下方式注册SimpleInjectorHttpDependencyResolver

// 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): 下面是适用于我的项目的代码片段,包括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));

To access your types in the API controllers you could use the following code snippet: 要访问API控制器中的类型,您可以使用以下代码段:

DependencyResolver.Current.GetService<InterfaceName>()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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