Error injecting framework repository into . net core

Asked

Viewed 61 times

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 already put the part of the instance

  • this part of the code says nothing about how you are injecting the dependency, please provide more information

  • 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?

  • I have already added the line services.Addtransient<Cotistarepositorio, Cotistarepositorio>(); in startup.Cs the error has changed. Could be some mysql configuration?

  • 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

  • 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

  • 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/

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.