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.

the_endian
  • 2,259
  • 1
  • 24
  • 49
  • As described in this SO answer: We should avoid using `IDependencyResolver` interface. https://stackoverflow.com/a/22058942/1169180 – Shaggy Nov 01 '18 at 17:41
  • As of 2019, Unity.AspNet.WebApi (https://www.nuget.org/packages/Unity.AspNet.WebApi/) solved my problem with WebAPI2 + Owin (Katana) + Unity. Library contain properly working UnityResolver in such case. – EKOlog Apr 16 '19 at 12:39

1 Answers1

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();
    }
}
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69