Asp.net web api web api中的统一接口映射异常

Asp.net web api web api中的统一接口映射异常,asp.net-web-api,unity-container,Asp.net Web Api,Unity Container,调试代码时出现此异常: Resolution of the dependency failed, type = "Aqueduct.Interfaces.IMasterDataClient", name = "MDS". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, Aqueduct.Interfaces.IMasterDataClien

调试代码时出现此异常:

Resolution of the dependency failed, type = "Aqueduct.Interfaces.IMasterDataClient", name = "MDS".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type,
Aqueduct.Interfaces.IMasterDataClient, is an interface and cannot be constructed. Are you missing a type mapping?
发生异常时,容器代码为:

解析导水管.接口.IMasterDataClient,MDS

Unity.config:

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
        IUnityContainer myContainer = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();
        myContainer.RegisterType<IMasterDataClient, MasterDataClient>("MDS");
        myContainer.RegisterType<ILinksManager, LinksManager>("LDS");
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}

这里有三个问题

问题1。您正在创建一个新容器:

using (var container = new UnityContainer())
{
       container.Resolve<IMasterDataClient>("MDS");
       container.Resolve<ILinksManager>("LDS");
}
这样可以避免服务定位器模式和对容器本身的依赖。如果使用命名注册,则可能需要使用
依赖项
-属性

问题1-解决方案2:注入容器而不是创建新容器。 如果出于某种原因确实需要控制器,那么应该尝试注入控制器。这样,您将获得与注册时使用的容器相同的容器

public class MyApiController : ApiController
{
    IUnityContainer _container;


    public MyApiController(IUnityContainer container)
    {
        _container = container;
    }
}
问题1-解决方案3:保持对容器的静态引用。 作为最后的手段,您可以将容器保持为静态实例

public static class IocContainer
{
    private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        return container;
    });

    public static IUnityContainer Instance
    {
        get { return Container.Value; }
    }
}
更新:

using (var container = new UnityContainer())
{
       container.Resolve<IMasterDataClient>("MDS");
       container.Resolve<ILinksManager>("LDS");
}
我不知道如何实现IapClient和IUriDispenser的类 它是第三方dll,我如何注册它

Unity需要知道在解析接口时使用哪个实现。因此,您需要告诉Unity要使用什么实现。下面的问题有一个很好的答案:

它扫描程序集以查找接口的实现,然后注册它们/它

或者,您可以使用,它使您能够同时进行多个注册:

container.RegisterTypes(
    AllClasses.FromLoadedAssemblies(),
    WithMapping.MatchingInterface,
    WithName.Default,
    WithLifetime.ContainerControlled);
注意,这将注册所有实现,包括您自己的实现。 如果您只需要第三方实现,您应该能够执行以下操作:

// Get the assemblies where IApiClient exists.
IEnumerable<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies()
    .Where(x => x.GetTypes().Contains(typeof (IApiClient)));

// Register all implementations based on convention.
container.RegisterTypes(AllClasses.FromAssemblies(assemblies), 
    WithMappings.FromMatchingInterface,
    WithName.Default,
    WithLifetime.ContainerControlled); // Maybe another lifetime manager?
//获取存在IApiClient的程序集。
数不清


主要是Unity告诉你,你正在试图解决一些你没有注册的问题。或者您根本没有注册DependencyResolver。

谢谢。我已经做了所有更改并应用了问题1的解决方案1。但是我收到了500个内部服务器错误[Stack trace error]尝试创建“WPProjectsController”类型的控制器时出错。确保控制器具有无参数公共构造函数。类型“WebService.Controllers.WPProjectsController”没有默认值constructor@smoksnes-现在我找到了那些实现类,所以我也注册了它们..谢谢我需要在UnityDependencyResolver类中添加一些东西吗..目前我还没有对此进行任何更改..我缺少一些依赖项设置吗?您需要创建一个
UnityDependencyResolver
。这里有一篇关于使用WebAPI建立Unity的精彩文章-
public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = IocContainer.Instance;

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();
        myContainer.RegisterType<IMasterDataClient, MasterDataClient>("MDS");
        myContainer.RegisterType<ILinksManager, LinksManager>("LDS");
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}
IocContainer.Instance.Resolve<IMasterDataClient>("MDS");
public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();
        container.RegisterType<IMasterDataClient, MasterDataClient>("MDS");
        container.RegisterType<ILinksManager, LinksManager>("LDS");
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}
container.RegisterTypes(
    AllClasses.FromLoadedAssemblies(),
    WithMapping.MatchingInterface,
    WithName.Default,
    WithLifetime.ContainerControlled);
// Get the assemblies where IApiClient exists.
IEnumerable<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies()
    .Where(x => x.GetTypes().Contains(typeof (IApiClient)));

// Register all implementations based on convention.
container.RegisterTypes(AllClasses.FromAssemblies(assemblies), 
    WithMappings.FromMatchingInterface,
    WithName.Default,
    WithLifetime.ContainerControlled); // Maybe another lifetime manager?