C# WebApi.UnityDependencyResolver 未实现 Microsoft.Practices.ServiceLocation.IServiceLocator。参数:commonServiceLocator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14012735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:23:54  来源:igfitidea点击:

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

c#asp.netasp.net-mvc-4asp.net-web-apiunity-container

提问by Elad Benda

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

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

我尝试安装 webApi 包,但没有帮助

采纳答案by Darin Dimitrov

In your Initializemethod replace:

在您的Initialize方法中替换:

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.

System.Web.Http.Dependencies.IDependencyResolver(由 Web API 使用)和System.web.Mvc.IDependencyResolver(由 ASP.NET MVC 使用)是两种完全不同的类型(即使它们具有相同的名称),但您尝试将它们分配给相同的类型 ( UnityDependencyResolver),这显然是行不通的。

回答by OaicStef

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

我有一个类似的问题,但就我而言,我的 Web 应用程序具有 MVC 和 WebApi 控制器。我是这样解决的:

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.

第一行将使用 MVC DependencyResolver,在第二行中我使用 WebApi UnityDependencyResolver。