13

I try to run the following code:

using System.Web.Http;
using System.Web.Mvc;
using Conduit.Mam.ClientSerivces.Dal.Configuration;
using MamInfrastructure.Common.Dal;
using MamInfrastructure.Common.Logger;
using MamInfrastructure.Logger;
using Microsoft.Practices.Unity;
using Unity.WebApi;

namespace Conduit.Mam.ClientServices.Common.Initizliaer
{
    public static class Initializer
    {
        private static bool isInitialize;
        private static readonly object LockObj = new object();
        private static IUnityContainer defaultContainer = new UnityContainer();

        static Initializer()
        {
            Initialize();
        }

        public static void Initialize()
        {
            if (isInitialize)
                return;

            lock (LockObj)
            {
                IUnityContainer container = defaultContainer;

                //registering Unity for web API
                GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
                //registering Unity for MVC
                DependencyResolver.SetResolver(new UnityDependencyResolver(container));

                container.RegisterType<IDal<ClientService.DAL.EntityFramework.MamConfiguration>, MamConfigurationDal>();
                container.RegisterType<IApplicationLogger, Log4NetLogger>();

                if (!isInitialize)
                {
                    isInitialize = true;
                }
            }
        }
    }
}

ad get the following exception:

The type Unity.WebApi.UnityDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator. Parameter name: commonServiceLocator

I have tried to install webApi package but it didn't help

Elad Benda
  • 35,076
  • 87
  • 265
  • 471

2 Answers2

30

In your Initialize method replace:

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

with:

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

Now you understand what the problem is:

System.Web.Http.Dependencies.IDependencyResolver (used by the Web API) and System.web.Mvc.IDependencyResolver (used by ASP.NET MVC) are two completely different types (even if they have the same name) and yet you attempt to assign both of them to the same type (UnityDependencyResolver) which obviously cannot work.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    It was `GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);` it didn't work – Elad Benda Dec 24 '12 at 08:52
  • What error did you get? Take a look at the following blog post: http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html – Darin Dimitrov Dec 24 '12 at 09:31
  • 3
    I wrote: ` GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);` same error: `The type Unity.WebApi.UnityDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator. Parameter name: commonServiceLocator` – Elad Benda Dec 24 '12 at 15:17
  • 1
    You seem to have messed up something with your project references/NuGets. Read the blog post I have linked to and try it on a new project: http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html Follow it step by step. – Darin Dimitrov Dec 24 '12 at 23:19
  • GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); it didn't work its not recognizing WebApi . its saying The type or namespace name 'WebApi' does not exist in the namespace 'Unity' (are you missing an assembly reference?) – Joy Acharya May 19 '16 at 08:59
  • 1
    Please add nuget package "Unity.WebAPI" in order to make it work. – Tuyen Nguyen Feb 22 '17 at 04:17
11

I had a similar issue, but in my case my web application has MVC and WebApi Controllers. I resolved like this:

using Microsoft.Practices.Unity;
using System.Web.Http;
using System.Web.Mvc;
using Unity.Mvc5;

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

Where the first line is going to use the MVC DependencyResolver and in the second line I use the WebApi UnityDependencyResolver.

OaicStef
  • 167
  • 2
  • 8
  • This answer works well, but making the code slightly more explicit in terms of which library is being used might make it more clear to those who come later. DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); – Josh Aug 21 '19 at 12:45