1
Hello! So, I already checked my classes in the bank, I successfully mirrored and started to assemble the functionalities of my webservice. With that, I came across the following mistake: InvalidOperationException: Unable to resolve service for type 'CadFuncionario.Interface.IFuncionarioRepositorio' while attempting to activate 'CadFuncionario.Controllers.FuncionarioController
Follow my handler :
[Produces("application/json")]
[Route("api/Home")]
public class FuncionarioController : Controller
{
private readonly IFuncionarioRepositorio _context;
public FuncionarioController(IFuncionarioRepositorio context)
{
_context = context;
}
[HttpGet]
public IEnumerable<Funcionario> GetAll()
{
return _context.ListarTodos();
}
}
My repository:
public interface IFuncionarioRepositorio
{
//CRUDs Funcionario
void Adicionar(Funcionario Func);
void AttFuncionario(Funcionario Func);
void DelFuncionario(int IdFunc);
IEnumerable<Funcionario> ListarTodos();
}
The implementation of my interface:
public class FuncionarioRepositorio : IFuncionarioRepositorio
{
private CadFuncContext _context;
public FuncionarioRepositorio(CadFuncContext context)
{
_context = context;
}
public void Adicionar(Funcionario Func)
{
_context.Add(Func);
_context.SaveChanges();
}
public void AttFuncionario(Funcionario Func)
{
_context.Update(Func);
_context.SaveChanges();
}
public void DelFuncionario(int IdFunc)
{
throw new NotImplementedException();
}
public IEnumerable<Funcionario> ListarTodos()
{
return _context.funcionario;
}
}
And the service, I’m adding so:
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkNpgsql().AddDbContext<CadFuncContext>(opt =>
opt.UseNpgsql(Configuration.GetConnectionString("Conn")));
services.AddMvc();
}
I’ll be looking forward to a little help
he can’t instantiate the
controller
because you need aIFuncionarioRepositorio context
. I think you’re making a mess with repository / context... and in your controller, if you’re going to use aFuncionarioRepositorio
needs to pass aCadFunContext
in the builder, and this is not happening.– Rovann Linhalis
I did it! It was +- that, it was in the injection of the dependencies same, I changed the name of some variables, redefined and was!
– Thiago Oliveira