Problem Injecting a Service into the Controller: An unhandled Exception occurred while Processing the request

Asked

Viewed 1,105 times

1

I’m getting error below when trying to inject a service into a controler. I already checked my configuration on Statup.Cs.

Error:

An unhandled Exception occurred while Processing the request. Invalidoperationexception: Unable to resolve service for type 'AE.Infra.Persistence.Aecontext' while attempting to Activate 'AE.Infra.Repository.Userrepositorylmpr.*

// Configurações da injeção de dependência
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options =>
    {
        options.RespectBrowserAcceptHeader = true;
        options.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("text/xml"));
        options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValue.Parse("application/json"));
    })
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
    .AddXmlSerializerFormatters();

    services.AddScoped<IUserService, UserServiceImpl>();

    services.AddScoped<IUserRepository, UserRepositoryImpl>();

    //Dependency Injection of GenericRepository
    services.AddScoped(typeof(IRepository<>), typeof(GenericRepository<>));           
} 

I also created an infra project within my Solution, where she owns a DBContext where I make the connection:

public partial class AEContext : DbContext
{
    // Configurações do contexto
}

My class UserRepositoryImpl extend a GenericRepository<User> and implements the interface IUserRepository. Follows a model of a test scenario:

public class UserRepositoryImpl : GenericRepository<User>, IUserRepository
{
    public UserRepositoryImpl(AEContext context) : base(context)
    {
    }

    public async Task<User> FindByEmail(string email)
    {
        return await _context.Users.SingleOrDefaultAsync(u => u.Email.Equals(email));
    }
}

Man Genericrepository is extremely simple, only for scenario test imprint:

public class GenericRepository<T> : IRepository<T> where T : BaseEntity
{

    protected readonly AEContext _context;
    private readonly DbSet<T> _dataSet;

    public GenericRepository(AEContext context)
    {
        _context = context;
        _dataSet = _context.Set<T>();
    }

    ...
}
  • 1

    You didn’t register the class AEContext. services.AddScoped<AEContext>();

  • Thanks man, it worked. Since yesterday trying to solve this problem and was not proceeding.

  • 1

    Quiet, you’re welcome. I posted an answer with a few more details.

  • There are people classifying the issue as bad... I didn’t understand anything! rsrsr

  • I think the error message as image was not a good idea, you could also shorten the code a little and leave only the most important points. I am no expert at writing questions here, but I will edit your post and try to improve its structure according to some "practices" that people usually like.

  • Blz! Thanks for the tip... I’ve always followed good practices here to the letter.

Show 1 more comment

1 answer

3


If you want instances to be created automatically, you need to record any and all dependencies on them. This includes registering an instance of AEContext to be injected into what you call Repository.

Btw, I’m not sure if you want the instance to be by scope or transient, I used the method AddScoped based on existing code.

So, it remains to do this in the method ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    //...
    services.AddScoped<AEContext>();
}

Browser other questions tagged

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