title: ASP.NET Core Dependency Injection error: Unable to resolve service for type while attempting to activate - Error is coming while calling viewcomponent
body: <p>I am creating viewcomponents and below are my all code..but when I call this viewcomponent, I am getting error like below..so any one can help me to solve this as well as explain me what exactly issue is.</p>
  <p>The error is :</p>
  <blockquote>
  <p>InvalidOperationException: Unable to resolve service for type
  'PerfumeStore.Data.Models.ShoppingCart' while attempting to activate
  'PerfumeStore.ViewComponents.ShoppingCartSummaryViewComponent'.</p>
  </blockquote>
  <p>First of all I have created one folder with name ViewComponents in that I have created new class with name ShoppingCartSummaryViewComponent.cs and below is code of that.</p>
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using PerfumeStore.Data.Models;
  using PerfumeStore.ViewModels;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace PerfumeStore.ViewComponents
  {
      public class ShoppingCartSummaryViewComponent : ViewComponent
      {
          private readonly ShoppingCart _shoppingCart;
          public ShoppingCartSummaryViewComponent(ShoppingCart shoppingCart)
          {
              _shoppingCart = shoppingCart;
          }
  
          public IViewComponentResult Invoke()
          {
              var items = _shoppingCart.GetShoppingCartItems();
              _shoppingCart.ShoppingCartItems = items;
  
              var shoppingCartViewModel = new ShoppingCartViewModel
              {
                  ShoppingCart = _shoppingCart,
                  ShoppingCartTotal = _shoppingCart.GetShoppingCartTotal()
              };
  
              return View(shoppingCartViewModel);
          }
      }
  }
  </code></pre>
  <p>Now cshtml file is below in this path : in Views / Shared / Components / ShoppingCartSummary /Default.cshtml</p>
  <pre><code>@model ShoppingCartViewModel
  
  @if (Model.ShoppingCart.ShoppingCartItems.Count &gt; 0)
  {
      &lt;li&gt;
          &lt;a asp-controller=&quot;ShoppingCart&quot;&gt;
              &lt;span class=&quot;glphyicon glphyicon-shopping-cart&quot;&gt;&lt;/span&gt;
              &lt;span id=&quot;cart-status&quot;&gt;
                  @Model.ShoppingCart.ShoppingCartItems.Count
              &lt;/span&gt;
          &lt;/a&gt;
      &lt;/li&gt;
  }
  </code></pre>
  <p>Now in my _Layout.cshtml file I am calling this viewcomponent in this way.</p>
  <pre><code>&lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarSupportedContent&quot;&gt;
              &lt;ul class=&quot;navbar-nav mr-auto&quot;&gt;
                  @await Component.InvokeAsync(&quot;ShoppingCartSummary&quot;)
              &lt;/ul&gt;
          &lt;/div&gt;
  </code></pre>
  <p>But I am getting this error :</p>
  <p>InvalidOperationException: Unable to resolve service for type 'PerfumeStore.Data.Models.ShoppingCart' while attempting to activate 'PerfumeStore.ViewComponents.ShoppingCartSummaryViewComponent'.</p>
  <p>Thanks</p>