2

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>();
2
  • 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? Commented Aug 28, 2019 at 8:30
  • @Stuart - Yes I registered my data context in startup.cs. All services have been registered in startup. Commented Sep 1, 2019 at 18:20

1 Answer 1

4

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");
});
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I did services.AddDbContext<DataContext>(x => x.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
You need to post more code then, everything looks ok in your question.
My error. I had methods for configureservices and configuredevelopmentservices and i hadn't registered the dependency in the configuredevelopmentservices method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.