0
votes

I'm creating a shopping cart controller first time, I'm stuck in this exception I know there is a need to add scoped service in startup class, but don't know how to do in my case

InvalidOperationException: Unable to resolve service for type 'fruittyPie.Models.ShoppingCartItem' while attempting to activate 'fruittyPie.Controllers.CartController'.

my startup:

public void ConfigureServices(IServiceCollection services)

   {
        services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddTransient<IPieRepository,  PieRepository>();
        services.AddTransient<ICatagoryRepository, CatagoryRepository>();
        services.AddTransient<IFeedBackRepository, FeedbackRepository>();
        services.AddSingleton<IFileProvider>(
            new PhysicalFileProvider(
                Path.Combine(
                    Directory.GetCurrentDirectory(), "wwwroot")
                    ));
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.AddMvc();
        services.AddMemoryCache();
        services.AddSession();
    }

CartController

  public class CartController : Controller

  {
    //[Route("cart")]
    private readonly AppDbContext _appDbContext;
    private readonly  ShoppingCartItem _shoppingCartItems;
    private readonly ShoppingCart _shoppingCart;

    public CartController(AppDbContext appDbContext,ShoppingCartItem shoppingCartItem, ShoppingCart shoppingCart)
    {
        _appDbContext = appDbContext;
        _shoppingCartItems = shoppingCartItem;
        _shoppingCart = shoppingCart;
    }

    public string ShoppingCartId { get; set; }

    public IActionResult  Index()
    {
       // List<ShoppingCartItem> shoppingCartItems = new List<ShoppingCartItem>();
        ViewBag.cart =
            _appDbContext.ShoppingCartItemx.Where
            (c => c.CartId == ShoppingCartId)
                .Include(s => s.productx)
                .ToListAsync();
        ViewBag.total = _appDbContext.ShoppingCartItemx.Where(d => d.CartId == ShoppingCartId)
            .Select(f => f.productx.Price * f.Amount).Sum();

        return View();
    }
    public void AddToCart(Product productz, int amount)
    {
        var shoppingCartItemz = _appDbContext.ShoppingCartItemx.SingleOrDefault(
            s => s.productx.Id == productz.Id && s.CartId == ShoppingCartId);

        if (shoppingCartItemz == null)
        {
            shoppingCartItemz = new ShoppingCartItem
            {
                CartId = ShoppingCartId,
                productx = productz,
                Amount = 1
            };
            _appDbContext.ShoppingCartItemx.Add(shoppingCartItemz);
        }

        else
        {
            shoppingCartItemz.Amount++;
        }
        _appDbContext.SaveChanges();
    }

    public int RemoveFromCart(Product productz)
    {
        var shoppingCartItemz = _appDbContext.ShoppingCartItemx.SingleOrDefault(
            s => s.productx.Id == productz.Id && s.CartId == ShoppingCartId);
        var localAmount = 0;

        if (shoppingCartItemz != null)
        {
            if (shoppingCartItemz.Amount > 1)
            {
                shoppingCartItemz.Amount--;
                localAmount = shoppingCartItemz.Amount;
            }
            else
            {

                _appDbContext.ShoppingCartItemx.Remove(shoppingCartItemz);
            }
        }
        _appDbContext.SaveChanges();
        return localAmount;
    }
}

}

1

1 Answers

1
votes

ShoppingCartItem and ShoppingCart are not registered as services but you added them to your constructor. ASP.NET will now try to resolve those parameters from the dependency injection container, which obviously fails.

These parameters do not belong in the constructor. You can remove them and query the desired ShoppingCartItem and/or ShoppinCart entities from the AppDbContext.