【问题标题】:Unity: The current type,'XXXXX', is an interface and cannot be constructed. Are you missing a type mapping?unity:当前类型,'XXXXX',是接口,不能构造。您是否缺少类型映射?
【发布时间】:2016-08-24 20:50:03
【问题描述】:

我有多个从同一个接口派生的类型。我正在使用 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,NameA

出于某些正当原因,我不想使用 Unity 的按约定注册选项或 Unity 的配置文件选项来注册类型。我想根据我拥有的列表注册它们。

【问题讨论】:

    标签: c# inversion-of-control unity-container ioc-container


    【解决方案1】:

    您忘记指定映射 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
    {
    
    }
    
    }
    

    【讨论】:

      【解决方案2】:

      您错误地使用了依赖注入。正确的方法是让你的控制器获取他们需要的依赖项,然后让依赖注入框架注入具体实例。 查找更多信息here

          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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-30
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2011-12-30
        相关资源
        最近更新 更多