进行简单的喷油器支持MVC 4的ASP.NET Web API?喷油器、简单、MVC、ASP

2023-09-04 00:07:06 作者:死亡婚纱

我是新来的简单的注射器IOC容器。我将开始一个项目,该项目将使用MVC 4的ASP.NET Web API需要一个多租户ASP.NET MVC实现的工作。

我的问题是:是否简单喷射器支持MVC 4的ASP.NET Web API?如这需要参考MVC 3,我想知道,如果MVC 4也被支持。

解决方案   

进行简单的注射器国际奥委会支持MVC 4的ASP.NET Web API?

它目前是MVC4的Web API的支持,但支持将在未来加入。当这种情况发生集成指南将被更新。

更新:的Web API支持已加入简单的喷油器2.5。

在此期间,你可以创建自己的 System.Web.Http.Dependencies.IDependencyResolver 实施简单注射器。下面是实施使用Web API在IIS托管环境中工作:

公共类SimpleInjectorHttpDependencyResolver:     System.Web.Http.Dependencies.IDependencyResolver {     私人只读集装箱货柜;     公共SimpleInjectorHttpDependencyResolver(         集装箱货柜)     {         this.container =容器;     }     公共System.Web.Http.Dependencies.IDependencyScope         BeginScope()     {         回到这一点;     }     公共对象GetService的(类型的serviceType)     {         IServiceProvider的提供商= this.container;         返回provider.GetService(的serviceType);     }     公开的IEnumerable<对象> GetServices(类型的serviceType)     {         IServiceProvider的提供商= this.container;         键入collectionType = typeof运算(IEnumerable的<>)MakeGenericType(的serviceType)。         VAR的服务=(IEnumerable的<对象>)this.ServiceProvider.GetService(collectionType);         返回服务? Enumerable.Empty&其中;对象>();     }     公共无效的Dispose()     {     } }

本实施实现了没有范围,因为你需要使用每个Web API请求终身实现内部网络托管环境(其中一个要求可能最终在不同的线程比在那里它开始)作用域。

Because的网络API设计的方式来显式注册所有的Web API控制器,​​这是非常重要的。您可以通过以下code做到这一点:

VAR的服务= GlobalConfiguration.Configuration.Services; VAR controllerTypes = services.GetHttpControllerTypeResolver()     .GetControllerTypes(services.GetAssembliesResolver()); 的foreach(在controllerTypes VAR controllerType) {     container.Register(controllerType); } ASP.NET MVC4中调用WEB API的四个方法

您可以注册 SimpleInjectorHttpDependencyResolver 如下:

//注意:要完成这个任务最后一步,在注册后的控制器。 GlobalConfiguration.Configuration.DependencyResolver =     新SimpleInjectorHttpDependencyResolver(容器);

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