我是ASP .net核心的新手。它给了我一个依赖项注入错误,如下所示:
InvalidOperationException:尝试激活“ SpinIt.Web.Controllers.CartController”时,无法解析类型为“ Spinit.Data.Services.Repository.GameRepository”的服务。 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,bool isDefaultParameterRequired)
StackTrace如下:
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 代码
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);
}
}
我还向 Startup.cs
添加了服务services.AddScoped<IGame, GameRepository>();
services.AddScoped<ICartItem, CartItemRepository>();
答案 0 :(得分:3)
您已在Startup.cs文件中使用.NET Core服务容器将IGame
注册为服务类型,并将GameRepository
注册为实现类型。因此,显然IGame
已注册在服务容器中,并且只能通过DI注入IGame
类型。 DI系统不知道名为GameRepository
的服务,因为它未在DI服务容器中注册。
请在控制器构造函数中进行如下更改:
private readonly IGame _gameRepository;
private readonly ICartItem _cartRepository;
public CartController(IGame gameRepository, ICartItem cartRepository)
{
_gameRepository = gameRepository;
_cartRepository = cartRepository;
}
答案 1 :(得分:2)
您正在使用实现作为构造函数参数,您没有在服务中提及它。尝试使用该界面。
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);
}
}