0

I want to inject unity container into WebController.

I have UnityDependencyResolver:

public class UnityDependencyResolver : IDependencyResolver
{
    readonly IUnityContainer _container;

    public UnityDependencyResolver(IUnityContainer container)
    {
    this._container = container;
    }

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

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

    public void Dispose()
    {
        _container.Dispose();
    }
}

Then, in my Global.asax I add the following line:

var container = new UnityContainer();
container.RegisterType<IService, Service>
(new PerThreadLifetimeManager()).RegisterType<IDALContext, DALContext>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));

Then, If I use the following in a Web Controller:

private IService _service;

public HomeController(IService srv)
{
    _service = srv;
}

It works fine.

But I want to inject it into WebAPI Controller, so if I do it the same way:

private IService _service;

public ValuesController(IService srv)
{
    _service = srv;
}

It does not work, it says that constructor is not defined. Ok, I create one more constructor:

public ValuesController(){}

And in this case it uses only this constructor and never the one where I should inject unity container.

Please advise.

2
  • Is this a WebAPI project or MVC app that happens to have an API controller.
    – jpgrassi
    Jan 21, 2016 at 20:15
  • This is a MVC project that has API Controller.
    – J.Doe
    Jan 22, 2016 at 6:54

1 Answer 1

1

Add this in your WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Routes and other stuff here...

        var container = IocContainer.Instance; // Or any other way to fetch your container.
        config.DependencyResolver = new UnityDependencyResolver(container);
    }
}

And if you want the same container you can keep it in a static variable, like so:

public static class IocContainer
{
    private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        return container;
    });

    public static IUnityContainer Instance
    {
        get { return Container.Value; }
    }
}

More info can be found here:

http://www.asp.net/web-api/overview/advanced/dependency-injection

On a sidenote, I can also recommend the nuget-package Unity.Mvc. It adds a UnityWebActivator and support for PerRequestLifetimeManager.

https://www.nuget.org/packages/Unity.Mvc/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.