I can’t do addiction injection

Asked

Viewed 271 times

2

I’m working with ASP.NET CORE 2 and is a layered application.

This is my ConfigureServices in the Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<DbContext, ApplicationContext>();
        services.AddTransient<IUsuarioRepository, UsuarioRepository>();
        services.AddTransient<IUsuarioService, UsuarioService>();
       services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

Error that appears when starting apilication (Updated):

Invalidoperationexception: Unable to resolve service for type 'ddd.Infrastructure.Data.Applicationcontext' while attempting to Activate 'ddd.Infrastructure.Repositories.Usuariorepository'.

Follows my UsuarioRepository

public class UsuarioRepository : Repository<Usuario>, IUsuarioRepository
{
    private readonly ApplicationContext _context;

    public UsuarioRepository(ApplicationContext context) : base(context)
    {
        _context = context;
    }

    public bool ValidarUsuarioSenha(Usuario usuario)
    {
        return true;
    }
}

Follows my ApplicationContext

public class ApplicationContext : DbContext
{
    private string strConnection
    ="";

    public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
    {

    }

    public DbSet<Usuario> Usuarios { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseNpgsql(strConnection);
        }
    }
}

*Updated Question.

3 answers

1

Two injections are missing for your code to work correctly, 1º of the Repository and 2º of your context, your code should be as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<DbContext, ApplicationContext>();
    services.AddTransient<IUsuarioRepository, UsuarioRepository>();
    services.AddTransient<IUsuarioService, UsuarioService>();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

In his UsuarioRepository and in the Repository use the DbContext instead of ApplicationContext because in addiction injection you are "saying" when you need a DbContext use the ApplicationContext

public class UsuarioRepository : Repository<Usuario>, IUsuarioRepository
{
    private readonly DbContext _context;

    public UsuarioRepository(DbContext context) : base(context)
    {
        _context = context;
    }
}

//Sua classe deve estar diferente disso, é apenas um exemplo
public class Repository
{
    private DbContext _context;

    public Repository(DbContext context) : base(context)
    {
        _context = context;
    }
}
  • didn’t work... that DbContext is EF core, correct? did not work

  • Yes, what error message?

  • InvalidOperationException: Unable to resolve service for type 'ddd.Infrastructure.Data.ApplicationContext' while attempting to activate 'ddd.Infrastructure.Repositories.UsuarioRepository'.

  • It would be possible to add in your question contexto and the class UsuarioRepository and also the ConfigureServices up-to-date

  • OK I updated the question

0

Your service user gets right Iusuariorepository, in this case you have to pass

   services.AddTransient<IUsuarioRepository, UsuarioRepository>();
   services.AddSingleton<IUsuarioService, UsuarioService>();

It will pass the Usuariorepository to the Usuarioservice and Usuarioservice to the controller. Test it and tell me if it worked...

0

In the Startup.Cs file you need to also add Iusuariorepository in dependency injection, your file would look like this:

public void ConfigureServices(IServiceCollection services)
{
       services.AddTransient<IUsuarioRepository, UsuarioRepository>();
       services.AddTransient<IUsuarioService, UsuarioService>();
       services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
  • I get it.. now you’re asking me for another dependency injection... you have to have all the DI’s in that method?

  • Now there’s this mistake in the layer: Unable to resolve service for type 'ddd.Infrastructure.Data.ApplicationContext' while attempting to activate 'ddd.Infrastructure.Repositories.UsuarioRepository

  • Your Usersrepository must have as argument your Applicationcontext, so you need to register it in your Servicecollection yes

  • Thus: services.AddTransient<IUsuarioRepository, ApplicationContext>(); ?? You’re making a mistake on this line

  • No interface for Applicationcontext?

  • So.. don’t have... need? On Applicationcontext is my Context.. has to have an interface?

  • SM_S, add the line services.Addtransient<Applicationcontext, Applicationcontext>(); soon after giving the Addtransient in Dbcontext

Show 2 more comments

Browser other questions tagged

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