Cannot Inject Dependencies into ASP.NET Web API Controller using Unity

别说谁变了你拦得住时间么 提交于 2019-11-27 17:01:01
Oved D

Figured it out.

For ApiControllers, MVC 4 uses a System.Web.Http.Dispatcher.IHttpControllerFactory and System.Web.Http.Dispatcher.IHttpControllerActivator to create the controllers. If there is no static method to register what the implementation of these they are; when they are resolved, the mvc framework looks for the implementations in the dependency resolver, and if they are not found, uses the default implementations.

I got unity resolution of controller dependencies working by doing the following:

Created a UnityHttpControllerActivator:

public class UnityHttpControllerActivator : IHttpControllerActivator
{
    private IUnityContainer _container;

    public UnityHttpControllerActivator(IUnityContainer container)
    {
        _container = container;
    }

    public IHttpController Create(HttpControllerContext controllerContext, Type controllerType)
    {
        return (IHttpController)_container.Resolve(controllerType);
    }
}

Registered that controller activator as the implementation in the unity container itself:

protected void Application_Start()
{
    // code intentionally omitted

    IUnityContainer container = BuildUnityContainer();
    container.RegisterInstance<IHttpControllerActivator>(new UnityHttpControllerActivator(container));

    ServiceResolver.SetResolver(t =>
       {
         // rest of code is the same as in question above, and is omitted.
       });
}

You might want to take a look at the Unity.WebApi NuGet package which will sort all this out and also cater for IDisposable components.

see

http://nuget.org/packages/Unity.WebAPI

or

http://www.devtrends.co.uk/blog/introducing-the-unity.webapi-nuget-package

Microsoft have created a package for this.

Run the following command from package manager console.

install-package Unity.AspNet.WebApi

If you already have Unity installed, it will ask if you want to overwrite App_Start\UnityConfig.cs. Answer no and continue.

No need to change any other code and DI (with unity) will work.

Vincent

I had the same error and was searching the internet for solutions a couple of hours. Eventually it appeared that I had to register Unity BEFORE I was calling the WebApiConfig.Register. My global.asax now looks like

public class WebApiApplication : System.Web.HttpApplication
{
   protected void Application_Start()
   {
       UnityConfig.RegisterComponents();
       AreaRegistration.RegisterAllAreas();
       GlobalConfiguration.Configure(WebApiConfig.Register);
       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
       RouteConfig.RegisterRoutes(RouteTable.Routes);
       BundleConfig.RegisterBundles(BundleTable.Bundles);
   }
}

For me this solved the issue that Unity could not resolve the dependencies in my controllers

In a recent RC, I find that there is no longer a SetResolver method. To enable both IoC for controller and webapi, I use Unity.WebApi (NuGet) and the following code:

public static class Bootstrapper
{
    public static void Initialise()
    {
        var container = BuildUnityContainer();

        GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

        ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new ControllerActivator()));
    }

    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();

        container.Configure(c => c.Scan(scan =>
        {
            scan.AssembliesInBaseDirectory();
            scan.With<UnityConfiguration.FirstInterfaceConvention>().IgnoreInterfacesOnBaseTypes();
        }));

        return container;
    }
}

public class ControllerActivator : IControllerActivator
{
    IController IControllerActivator.Create(RequestContext requestContext, Type controllerType)
    {
        return GlobalConfiguration.Configuration.DependencyResolver.GetService(controllerType) as IController;
    }
}

I also use UnityConfiguration (also from NuGet) for IoC magic. ;)

After reading the answers, I still had to do a lot of digging around to land at this gotcha so here it is for the benefit of peers: This is all you need to do in ASP.NET 4 Web API RC (as on 8th August '13):

  1. Add a reference to "Microsoft.Practices.Unity.dll" [I'm on version 3.0.0.0, added through NuGet]
  2. Add a reference to "Unity.WebApi.dll" [I'm on version 0.10.0.0, added through NuGet]
  3. Register your type mappings with the container - similar to the code in Bootstrapper.cs that gets added to your project by Unity.WebApi project.
  4. In the controllers that inherit from the ApiController class, create parameterized constructors that have their parameter types as the mapped types

Lo and behold, you get the dependencies injected into your constructor without another line of code!

NOTE: I got this information from one of the comments in THIS blog by it's author.

I had the same exception being thrown, and in my case I had a conflict between MVC3 and MVC4 binaries. This was preventing my controllers from being registered properly with my IOC container. Check your web.config and make sure it's pointed to the correct versions of MVC.

Srihari Sridharan

This issue occurs due to registering Controllers with Unity. I solved this by using registration by convention as shown below. Please filter out any additional types as required.

    IUnityContainer container = new UnityContainer();

    // Do not register ApiControllers with Unity.
    List<Type> typesOtherThanApiControllers
        = AllClasses.FromLoadedAssemblies()
            .Where(type => (null != type.BaseType)
                           && (type.BaseType != typeof (ApiController))).ToList();

    container.RegisterTypes(
        typesOtherThanApiControllers,
        WithMappings.FromMatchingInterface,
        WithName.Default,
        WithLifetime.ContainerControlled);

Also the example above uses AllClasses.FromLoadedAssemblies(). If you are looking at loading assemblies from base path, it might not work as expected in a Web API project using Unity. Please take a look at my response to another question related to this. https://stackoverflow.com/a/26624602/1350747

Short summary for ASP.NET Web API 2.

Install Unity from NuGet.

Create a new class called UnityResolver:

using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;

public class UnityResolver : IDependencyResolver
{
    protected IUnityContainer container;

    public UnityResolver(IUnityContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }
        this.container = container;
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return container.Resolve(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return container.ResolveAll(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return new List<object>();
        }
    }

    public IDependencyScope BeginScope()
    {
        var child = container.CreateChildContainer();
        return new UnityResolver(child);
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        container.Dispose();
    }
}

Create a new class called UnityConfig:

public static class UnityConfig
{
    public static void ConfigureUnity(HttpConfiguration config)
    {
        var container = new UnityContainer();
        container.RegisterType<ISomethingRepository, SomethingRepository>();
        config.DependencyResolver = new UnityResolver(container);
    }
}

Edit App_Start -> WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    UnityConfig.ConfigureUnity(config);
    ...

Now it will work.

Original source but a bit modified: https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection

I had the same issue when using the Unity.WebAPI NuGet package. The problem was that the package never added a call to UnityConfig.RegisterComponents() in my Global.asax.

Global.asax.cs should look like this:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        UnityConfig.RegisterComponents();
        ...
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!