异常是:InvalidOperationException - 当前类型 log4net.ILog 是一个接口,无法构造。您是否缺少类型映射?

Exception is: InvalidOperationException - The current type, log4net.ILog, is an interface and cannot be constructed. Are you missing a type mapping?

我正在使用 unity-container.I 已将我的 class 注册到 IOC 容器。当我尝试创建 Auth class 的实例时,出现以下异常: 异常是:InvalidOperationException - 当前类型 log4net.ILog 是一个接口,无法构造。您是否缺少类型映射?

如果我做错了什么请告诉我?

我的代码:

    using Unity;
    Public Main()
    {
     private readonly IUnityContainer container;
     public override void Instantiate()
     {
       container.RegisterSingleton<IAuthentication, Auth>("Auth");
     }

     public Authenticate()
     {
       var instance = container.Resolve<IAuthentication>("Auth");**//Getting exception here**
     }
    }

授权 class:

    public class Auth: IAuthentication
    {
      private readonly ILog log;
      private IImpID impIDobj;
      public Auth(ILog log, IImpID impIDobj)
        {
            this.impIDobj= impIDobj;
            this.log = log;
        }

        public Auth()
            : this(LogManager.GetLogger("Auth"), new CAuth())
        {
        }
        public Authenticate()
        {
          impIDobj.Authenticate(data);
         //Some logics
        }
    }

容器不知道 ILog 的类型是什么,也无法创建 Auth 的实例。 请提供 ILog 的类型,将此行添加到您的 Instantiate 方法中。

container.RegisterSingleton<ILog, yourOwnLogType>();

感谢大家的反馈。但是我可以用 "InjectionConstructor" 解决这个问题。更多信息是 here.

我的新授权 class:

 public class Auth: IAuthentication
{
  private readonly ILog log;
  private IImpID impIDobj;
  public Auth(ILog log, IImpID impIDobj)
    {
        this.impIDobj= impIDobj;
        this.log = log;
    }

    [InjectionConstructor]
    public Auth()
        : this(LogManager.GetLogger("Auth"), new CAuth())
    {
    }
    public Authenticate()
    {
      impIDobj.Authenticate(data);
     //Some logics
    }
}