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>();
}
...
}
You didn’t register the class
AEContext
.services.AddScoped<AEContext>();
– Jéf Bueno
Thanks man, it worked. Since yesterday trying to solve this problem and was not proceeding.
– Thiago Cunha
Quiet, you’re welcome. I posted an answer with a few more details.
– Jéf Bueno
There are people classifying the issue as bad... I didn’t understand anything! rsrsr
– Thiago Cunha
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.
– Jéf Bueno
Blz! Thanks for the tip... I’ve always followed good practices here to the letter.
– Thiago Cunha