WebApi.UnityDependencyResolver does not implement Microsoft.Practices.ServiceLocation.IServiceLocator. Parameter : commonServiceLocator

后端 未结 2 1484
Happy的楠姐
Happy的楠姐 2021-02-20 09:54

I try to run the following code:

using System.Web.Http;
using System.Web.Mvc;
using Conduit.Mam.ClientSerivces.Dal.Configuration;
using MamInfrastructure.Common.         


        
相关标签:
2条回答
  • 2021-02-20 10:35

    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.

    0 讨论(0)
  • 2021-02-20 10:43

    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.

    0 讨论(0)
提交回复
热议问题