0
votes

I'm new to ASP .net core. It giving me an dependency injection error as:

InvalidOperationException: Unable to resolve service for type 'Spinit.Data.Services.Repository.GameRepository' while attempting to activate 'SpinIt.Web.Controllers.CartController'. Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

StackTrace is below:

Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
lambda_method(Closure , IServiceProvider , object[] )
Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider+<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

CartController Code

public class CartController:Controller
{
    private readonly GameRepository _gameRepository;
    private readonly Cart _cartRepository;

    public CartController(GameRepository gameRepository, Cart cartRepository)
    {
        _gameRepository = gameRepository;
        _cartRepository = cartRepository;
    }

    public ViewResult Index()
    {
        var items = _cartRepository.GetCartItems();
        _cartRepository.CartItems = items;

        var sCVM = new CartViewModel
        {
            Cart = _cartRepository,
            CartTotal = _cartRepository.GetCartTotal()
        };
        return View(sCVM);
    }
}

I have also add service to Startup.cs

services.AddScoped<IGame, GameRepository>();
services.AddScoped<ICartItem, CartItemRepository>();
3

3 Answers

3
votes

You registered IGame as Service Type and GameRepository as implementation type with .NET Core service container in Startup.cs file. So apparently IGame is registered in service container and only IGame type can be injected through DI. DI system is not aware of service named GameRepository as it's not registered with DI service container.

Please make changes in controller constructor like so:

private readonly IGame _gameRepository;
private readonly ICartItem _cartRepository;

public CartController(IGame gameRepository, ICartItem cartRepository)
{
    _gameRepository = gameRepository;
    _cartRepository = cartRepository;
}
2
votes

You are using the implementation as a constructor argument, you did not mention it on the services. Try to use the interface.

public class CartController:Controller
{
    private readonly IGame _gameRepository;
    private readonly ICartItem _cartRepository;

    public CartController(IGame gameRepository, ICartItem cartRepository)
    {
        _gameRepository = gameRepository;
        _cartRepository = cartRepository;
    }

    public ViewResult Index()
    {
        var items = _cartRepository.GetCartItems();
        _cartRepository.CartItems = items;

        var sCVM = new CartViewModel
        {
            Cart = _cartRepository,
            CartTotal = _cartRepository.GetCartTotal()
        };
        return View(sCVM);
    }
}
0
votes

Replace GameRepository with IGame and CartItemRepository with ICartItem