Unity:当前类型'XXXXX'是一个接口,不能被构造.您是否缺少类型映射?

本文关键字:类型 不能 是否 映射 XXXXX 一个 Unity 接口 | 更新日期: 2023-09-27 18:04:02

我有多个从同一接口派生的类型。我使用Unity IOC容器来注册类型

public interface IService
{
}
public class ServiceA : IService
{
}
public class ServiceB : IService
{
}
public class ServiceC : IService
{
}

如果我注册这些类型如下

        container.RegisterType<IService, ServiceA>("NameA");
        container.RegisterType<IService, ServiceB>("NameB");
        container.RegisterType<IService, ServiceC>("NameC");

那么我就可以像下面这样解决类型,没有任何问题。

    var service = container.Resolve<IService>("NameA");

然而,我正在获得需要从外部与容器注册的类型列表。(让我们假设从文本文件)。所以我只需要注册那些在提供的列表中的类型。

public class Program
{
    public static void Main()
    {
        // i will be getting this  dictionary values from somewhere outside of application
        // but for testing im putting it here
        var list = new Dictionary<string, string>();
        list.Add("NameA", "ServiceA");
        list.Add("NameB", "ServiceB");
        list.Add("NameC", "ServiceC");

        var container = new UnityContainer();
        var thisAssemebly = Assembly.GetExecutingAssembly();
        //register types only that are in the dictionary
        foreach (var item in list)
        {
            var t = thisAssemebly.ExportedTypes.First(x => x.Name == item.Value);
            container.RegisterType(t, item.Key);
        }
        // try to resolve. I get error here
        var service = container.Resolve<IService>("NameA");
    }
}

我收到异常

类型的未处理异常"Microsoft.Practices.Unity。发生了"ResolutionFailedException"Microsoft.Practices.Unity.dll

附加信息:依赖项解析失败,类型="ConsoleApplication1。IService", name = "NameA".

当:解析时发生异常

异常是:InvalidOperationException -当前类型,ConsoleApplication1。IService是接口,不能是接口构造。您是否错过了类型映射?


异常发生时,容器为:

解决ConsoleApplication1。IService,对

出于一些正当的原因,我不想使用Unity的注册约定选项,或Unity的配置文件选项来注册类型。我想根据我的名单给他们登记。

Unity:当前类型'XXXXX'是一个接口,不能被构造.您是否缺少类型映射?

您忘记指定映射IYourInterface -> YourClass

如此:

namespace ConsoleApplicationGrbage
{
class Program
{
    static void Main(string[] args)
    {
        var container = new UnityContainer();
        var list = new Dictionary<string, string>();
        list.Add("NameA", "YourClass");
        var thisAssemebly = Assembly.GetExecutingAssembly();
        var exT = thisAssemebly.ExportedTypes;
        //register types only that are in the dictionary
        foreach (var item in list)
        {
            var typeClass = exT.First(x => x.Name == item.Value);
            var ivmt = Type.GetType("ConsoleApplicationGrbage.IYourInterface");
            // --> Map Interface to ImplementationType
            container.RegisterType(ivmt, typeClass, item.Key);
            // or directly:
            container.RegisterType(typeof(IYourInterface), typeClass, item.Key);    
        }
        var impl = container.Resolve<IYourInterface>("NameA");
    }
}

public interface IYourInterface
{
}
public class YourClass: IYourInterface
{
}
}

您错误地使用了依赖注入。正确的方法是让你的控制器获取它们需要的依赖,并让依赖注入框架注入具体的实例。点击这里查看更多信息。

    public class HomeController: Controller
    {
       private readonly ISettingsManager settingsManager;
       public HomeController(ISettingsManager settingsManager)
    {
        this.settingsManager = settingsManager;
    }
    public ActionResult Index()
    {
        // you could use the this.settingsManager here
    }
}