2017-12-13 4 views
0

Ich verwende Unity-Container. Ich habe meine Klasse zu IOC-Container registrieren. Wenn ich versuche, Instanz der Auth-Klasse zu erstellen, erhalte ich folgende Ausnahme: Ausnahme ist: InvalidOperationException - Der aktuelle Typ log4net.ILog ist eine Schnittstelle und kann nicht erstellt werden. Vermissen Sie ein Typ-Mapping?Ausnahme ist: InvalidOperationException - Der aktuelle Typ log4net.ILog ist eine Schnittstelle und kann nicht erstellt werden. Vermissen Sie ein Typ-Mapping?

Bitte lassen Sie mich wissen, wenn ich etwas falsch mache?

Mein Code:

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

Auth Klasse:

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

Voll Ausnahme: Ausnahme ist: InvalidOperationException - Der aktuelle Typ, log4net.ILog, ist eine Schnittstelle und kann nicht aufgebaut werden. Vermissen Sie eine Typzuordnung Zum Zeitpunkt der Ausnahme war der Container: Auflösen von Auth.IAuthentication [], (keine) Auflösen von Auth.IAuthentication, Auth Auflösen von ImpID.Auth, Auth Auflösen des Parameters 'log' von Konstruktor ImID.Auth (log4net.ILog log, IImpID impIDobj) Auflösen log4net.ILog, (keine) –

+0

Wenn Sie weitere Informationen haben, dann bearbeiten Sie die Frage, nicht in einen Kommentar. – juharr

+1

Wo haben Sie Ihre Protokollierungskomponente (n) mit dem Container registriert? – Amy

Antwort

0

Dank euch allen für Feedback. Aber ich kann dieses Problem mit "InjectionConstructor" lösen. Weitere Informationen sind here.

Meine neue Auth Klasse:

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

Der Container tut wissen, was der Typ für ILog ist, und kann nicht eine Instanz von Auth erstellen. Geben Sie den Typ für ILog an, und fügen Sie diese Zeile in Ihre Instantiate-Methode ein.

container.RegisterSingleton<ILog, yourOwnLogType>();

Verwandte Themen