2
votes

I am getting a dependancy injection error when i try to access my controller. the error is 'unable to resolve service for IOrderPartRepository while attempting to activate. I followed examples from tutorials, but am still not sure why it won't resolve. I reviewed similar questions, but it looked like i had incorporated the fix from similar questions in my solution

Error: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3] Route matched with {action = "GetOrderParts", controller = "OrderPart"}. Executing controller action with signature System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.IActionResult] GetOrderParts(DatingApp.API.Helpers.UserParams) on controller DatingApp.API.Controllers.UsersController (DatingApp.API). info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2] Executed action DatingApp.API.Controllers.UsersController.GetOrderParts (DatingApp.API) in 3.2277ms fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.InvalidOperationException: Unable to resolve service for type 'DatingApp.API.Data.IOrderPartRepository' while attempting to activate 'DatingApp.API.Controllers.OrderPartController'.

Here is the code

public interface IOrderPartRepository
{
     void Add<T>(T entity) where T: class;
     void Delete<T>(T entity) where T: class;
     Task<bool> SaveAll();
     Task<PagedList<OrderPart>> GetOrderParts(UserParams userParams);
     Task<OrderPart> GetOrderPart(int id);
}

public class OrderPartRepository: IOrderPartRepository
{
    private readonly DataContext _context;


    public OrderPartRepository(DataContext context)
    {
        _context = context;
    }
    public void Add<T>(T entity) where T : class
    {
        _context.Add(entity);
    }

    public void Delete<T>(T entity) where T : class
    {
        _context.Remove(entity);
    }

    public async Task<OrderPart> GetOrderPart(int id)
    {
        var orderPart = await _context.OrderParts.Include(p => p.Photos).FirstOrDefaultAsync(u => u.Id == id);

        return orderPart;
    }

    public async Task<PagedList<OrderPart>> GetOrderParts(UserParams userParams)
    {
        var orderparts = _context.OrderParts.Include(p => p.Photos)
            .OrderByDescending(u => u.Added).AsQueryable();


        if (!string.IsNullOrEmpty(userParams.OrderBy))
        {
            switch (userParams.OrderBy)
            {
                case "created":
                    orderparts = orderparts.OrderByDescending(u => u.Added);
                    break;
                default:
                    orderparts = orderparts.OrderByDescending(u => u.Added);
                    break;
            }
        }

        return await PagedList<OrderPart>.CreateAsync(orderparts, userParams.PageNumber, userParams.PageSize);
    }

    public async Task<bool> SaveAll()
    {
        return await _context.SaveChangesAsync() > 0;
    }

}
}

controller

private readonly IOrderPartRepository _repo;

public OrderPartController(IOrderPartRepository repo)
{
   this._repo = repo;
}

startup

services.AddScoped<IAuthRepository, AuthRepository>();
services.AddScoped<IDatingRepository, DatingRepository>();
services.AddScoped<IOrderPartRepository, OrderPartRepository>();
1
I took the majority of your code, substituting DataContext with one in a project I already had, and left out code where you have other classes which you didn't include and it seemed to work fine. So, have you registered your DataContext in Startup.cs? Also, do any of your other classes such as PagedList have any constructors that take services that would need registering?Stuart
@Stuart - Yes I registered my data context in startup.cs. All services have been registered in startup.Allan Rodsmith

1 Answers

2
votes

Did you register your DB context in the Startup class too? If not, that might be the reason of the problem, try something like:

services.AddDbContext<DataContext>(options => {
    options.UseSqlServer("Connection string");
});