0

Unten sind meine verschiedenen Klassendeklarationen und wie ich versuche, die Unity-Container-Konfiguration einzurichten, um eine Schnittstelle zur Concrete-Klassenimplementierung zu erhalten. Der Code löst derzeit entweder eine stackoverflow-Ausnahme aus oder schlägt vor, dass eine Schnittstelle nicht erstellt werden kann.Einstellen der richtigen Unity Container-Konfiguration zum Auflösen der Interface-Klasse im Dekorationsmuster

Bitte helfen Sie mir, entweder die Klassenstruktur oder die Containerkonfiguration zu beheben.

CodesController Klasse -

public class CodesController : ApiController 
{ 
    private readonly IUnitOfWorkAsync unitOfWork; 
    private readonly ICodeRepository repository; 

    public CodesController(IUnitOfWorkAsync unitOfWork, ICodeRepository codeRepository) 
    { 
     if (unitOfWork == null) 
     { 
      throw new ArgumentNullException("unitOfWork"); 
     } 

     this.unitOfWork = unitOfWork; 
     this.repository = codeRepository; 
    } 

//Other class level methods here 
} 

CodeRepository Klasse -

public class CodeRepository : ICodeRepository 
{ 
    private readonly ICodeRepository codeRepository; 

    public CodeRepository(ICodeRepository repository) 
    { 
     this.codeRepository = repository; 
    } 

    public virtual async Task<IEnumerable<Code>> GetCodeAsync(string codeKey) 
    { //Some implementation here} 
} 

ICodeRepository Schnittstelle -

public interface ICodeRepository : IRepositoryAsync<Code> 
{ 
    Task<IEnumerable<Code>> GetCodeAsync(string codeKey); 
} 

IRepositoryAsync Interface -

public interface IRepositoryAsync<TEntity> : IRepository<TEntity> where TEntity : class, IPersistenceHint 
{ 
    Task<bool> DeleteAsync(params object[] keyValues); 
    Task<bool> DeleteAsync(CancellationToken cancellationToken, params object[] keyValues); 
    Task<TEntity> FindAsync(params object[] keyValues); 
    Task<TEntity> FindAsync(CancellationToken cancellationToken, params object[] keyValues); 
} 

Unity Container Configuration-

container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(
      "test", 
      new TransientLifetimeManager(), 
      new InjectionConstructor(container.Resolve<IDataContextAsync>("test"))); 

container.RegisterType<ICodeRepository, CodeRepository>(); 
container.RegisterType<CodesController, CodesController>(); 

With this given configuration and class structure, based on my experimentation with container config, I get following exception -

JSON 
exceptionMessage=An error occurred when trying to create a controller of type 'CodesController'. Make sure that the controller has a parameterless public constructor. 
exceptionType=System.InvalidOperationException 
innerException 
exceptionMessage=Type '<Namespace>.Api.Controllers.CodesController' does not have a default constructor 
stackTrace= at System.Linq.Expressions.Expression.New(Type type) 
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) 

Please suggest, if anything is wrong here, so that I can fix the same. Already struggling many days on this.

+0

Bitte zeigen Sie die Ausnahmedetails – qujck

+0

Die Ausnahmedetails oben in der ursprünglichen Frage hinzugefügt. Hoffe das hilft –

+0

Siehe auch [diese Frage] (http://stackoverflow.com/questions/6109646/how-doi-i-use-the-decorator-pattern-withunity-without-explicitly-specifying-ever) und Ich mag besonders die Antwort mit [diesem Artikel] (http://www.beefycode.com/post/Decorator-Unity-Container-Extension.aspx). – TylerOhlsen

Antwort

0

You're injecting ICodeRepository bis CodeRepository, was wahrscheinlich zu stackoverflow-Ausnahme führt, da es weiterhin ICodeRepositories generieren wird. Es wird einen rekursiven Aufruf generieren. Etwas wie diese:

public class BaseFoo 
{ 
    public BaseFoo(BaseFoo foo){ } 
} 

public class Foo : BaseFoo 
{ 
    public Foo() : base(new Foo()) { } 
} 

Und in Bezug auf die -exception „keinen Default-Konstruktor haben“, registriert haben Sie eine DependencyResolver für Web-API? Siehe nur eine dieser Fragen für detailliertere Informationen, wie es geht:

Using Unity with Web Api 2 gives error does not have a default constructor

Unity.WebApi | Make sure that the controller has a parameterless public constructor

ASP.Net MVC 4 Web API controller dosn't work with Unity.WebApi

Als Randbemerkung, sollten Sie nicht die CodesController in Ihrer Einheit registrieren müssen Anmeldung.

Verwandte Themen