Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 539
user1489941 Avatar asked Jun 28 '12 23:06

user1489941


People also ask

Does MVC support dependency injection?

The Dependency Injection (DI) Design PatternThe Dependency Resolver in ASP.NET MVC can allow you to register your dependency logic somewhere else (e.g. a container or a bag of clubs). The advantages of using Dependency Injection pattern and Inversion of Control are the following: Reduces class coupling.

What is MVC view injection?

View injection can be useful for populating UI elements, like selection list, radio buttons etc. This will increase code re-usability and keep your Controller clean by minimizing the amount of code required on Controllers. Reference. http://aspnetmvc.readthedocs.io/projects/mvc/en/latest/views/dependency-injection.html.

Does ASP NET support dependency injection?

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.


2 Answers

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>()
like image 21
Yuriy Silvestrov Avatar answered Oct 22 '22 20:10

Yuriy Silvestrov


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); 
like image 145
Steven Avatar answered Oct 22 '22 20:10

Steven