DI Unity Container分辨率错误

时间:2013-07-26 20:17:04

标签: dependency-injection unity-container

我正在尝试伪造企业库LogWriter,因此在运行单元测试时它不会执行任何日志记录,因此它在简化版本中看起来像这样:

public class MockLogger : ILogger
{
      public MockLogger()
      {
      }

      public void Write(object message)
      {        
      }

      public void Write(object message, string category)
      {     
      }

      public void Write(object message, string category, int eventId)
      { 
      }

      public void Write(object message, string category, int eventId, int priority)
      {
      }

      public void Write(object message, string category, int eventId, int priority,   TraceEventType severity)
      {
      }
}

public class Logger : ILogger
{
      LogWriter writer;
      public Logger()
      {
          writer = (new LogWriterFactory()).Create();

      }

      public Logger(LogWriter logWriter)
      {
          writer = logWriter;
      }

      public void Write(LogEntry logEntry)
      {
          writer.Write(logEntry);
      }

      public void Write(object message)
      {
          writer.Write(message);
      }

      public void Write(object message, string category)
      {
          writer.Write(message, category);
      }

      public void Write(object message, string category, int eventId)
      {
          writer.Write(message, category, eventId);
      }

      public void Write(object message, string category, int eventId, int priority)
      {
          writer.Write(message, category, eventId, priority);
      }

      public void Write(object message, string category, int eventId, int priority,   TraceEventType severity)
      {
          writer.Write(message, category, priority, eventId, severity);
      }

      public bool ShouldLog(LogEntry logEntry)
      {
          return writer.ShouldLog(logEntry);
      }
}

然后我试图通过配置来解决它:

<unity xmlns="http://schemas.microsoft.com/practices/2012/unity">
<alias alias="ILogger" type="ConsoleApplicationTest.ILogger, ConsoleApplicationTest" />
<namespace name="ConsoleApplicationTest" />
<assembly name="ConsoleApplicationTest" />
<container>
  <register type="ConsoleApplicationTest.ILogger, ConsoleApplicationTest" name="logger" mapTo="ConsoleApplicationTest.Logger, ConsoleApplicationTest">
  </register>
</container>
<container name="Mock">
  <register type="ConsoleApplicationTest.ILogger, ConsoleApplicationTest" name="mockLogger" mapTo="ConsoleApplicationTest.MockLogger, ConsoleApplicationTest">
  </register>
</container>

var container = new UnityContainer();
container.LoadConfiguration();
var logger = container.Resolve<ILogger>();

我得到的错误如下:

  

依赖项的解析失败,type =   “ConsoleApplicationTest.ILogger”,name =“(none)”。发生了异常   while:同时解决。例外是:InvalidOperationException - The   类型ILogger没有可访问的构造函数。

这两个类都有一个无参数构造函数。我该怎么做才能解决这个错误?使用.NET 4.5

1 个答案:

答案 0 :(得分:3)

因为您已使用name="logger"name="mockLogger"注册了自己的类型,因此您需要在调用Resolve时传递名称:

var container = new UnityContainer();
container.LoadConfiguration();
var logger = container.Resolve<ILogger>("logger"); // or "mockLogger"