0
I’m having trouble trying to inject dependency into a repository. NET framework and API . NET Core and the problem below:
An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'DataAccessRf.Repositorio.CotistaRepositorio' while attempting to activate 'EMP.PainelCotista.WebService.Services.CotistaService'.
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound)
Where the repository "Dataaccessrf.Repositorio.Cotistarepositorio" is in . NET framework and "EMP.PainelCotista.Webservice.Services.Cotistaservice" is a web service on . NET core.
Follow the code of the instance:
private readonly Cotistarepositorio _cotistaRepositorio;
public CotistaService(CotistaRepositorio cotistaRepositorio) { _cotistaRepositorio = cotistaRepositorio; } public Cotista Authenticate(string email, string senha) { Cotista cotista = _cotistaRepositorio.AuthCotista(email, senha); if (cotista == null) throw new AppException("Dados de login incorretos"); return cotista; }
They would have some suggestion or hint to solve this problem. Thank you
present the code
– Leandro Angelo
Leandro Angelo already put the part of the instance
– Pedro
this part of the code says nothing about how you are injecting the dependency, please provide more information
– Lucas Miranda
The point is this Lucas. I’ve never injected the framework into the core. I could tell you what you need to do in startup.Cs to make this call?
– Pedro
I have already added the line services.Addtransient<Cotistarepositorio, Cotistarepositorio>(); in startup.Cs the error has changed. Could be some mysql configuration?
– Pedro
I believe your answer is found in the Microsoft documentation itself. Link: https://docs.microsoft.com/pt-br/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2
– Renzo Rodrigues
this Overload you are using is for when you have an interface and an implementation, as you are not using interface the right one would be: services.Addtransient<Cotistarepositorio>() only, as there is no more of your code I can not talk much more than that
– Lucas Miranda
In the future, to make it easier, you could use the scruter to inject the classes as you develop them so you don’t forget to inject something. https://andrewlock.net/using-scrutor-to-automatically-register-your-services-with-the-asp-net-core-di-container/
– Jozimar Back