2

I am following the procedures described on this website in order to set up a Unity Dependency Resolver for Dependency Injection into one of my controllers. This code works fine:

var container = new UnityContainer();
            container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());

The above code is placed in the WebApiConfig Register(HttpConfiguration config) method. However, the instructions also specify that you must use this code:

config.DependencyResolver = new DependencyResolver();

This is where the problem is: DependencyResolver() does not exist in the current context. I've tried searching for this and I also tried UnityDependencyResolver() which also does not exist. I am using (or have tried using) namespaces:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
using Microsoft.Practices.Unity;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using URLShortener.Models;
using Microsoft.Practices.Unity;
using System.Web.Http.Dependencies;

The DependencyResolver does not seem to exist anymore which has me baffled. Thank you.

2
  • As described in this SO answer: We should avoid using IDependencyResolver interface. stackoverflow.com/a/22058942/1169180 Commented Nov 1, 2018 at 17:41
  • As of 2019, Unity.AspNet.WebApi (nuget.org/packages/Unity.AspNet.WebApi) solved my problem with WebAPI2 + Owin (Katana) + Unity. Library contain properly working UnityResolver in such case. Commented Apr 16, 2019 at 12:39

1 Answer 1

5

The instructions specify that you should use this code:

config.DependencyResolver = new UnityResolver(container);

The UnityResolver class implements the IDependencyResolver interface.

And add the UnityResolver class

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()
    {
        container.Dispose();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The type or Namespace UnityResolver could not be found. And yes, I did an install-package Unity before all of this. Unity 4.0.1.
Great thank you! The instruction weren't very clear to me that I had to actually create this class.
@TeeSee that part is easy to miss. Great that it works now, happy to help! Cheers

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.