title: Unable to resolve service for type at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider
body: <p>I am attempting to receive data from the server controller, stocks.  </p>
  
  <p>I get this error:</p>
  
  <p>"System.InvalidOperationException: Unable to resolve service for type myBackEnd.Models.StockContext' while attempting to activate  'myBackEnd.Controllers.StockController'.
      at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService
      (IServiceProvider sp, Type type, Type requiredBy, Boolean 
      isDefaultParameterRequired"</p>
  
  <p>Here is my stocks controller code:</p>
  
  <pre><code>namespace myBackEnd.Controllers
  {
  [Route("api/stock")]
  [Produces("application/json")]
  
  public class StockController : ControllerBase
  {
      private readonly int fastEmaPeriod = 10;
  
      private readonly IHttpClientFactory _httpClientFactory;
      private readonly Models.StockContext _context;
  
      public StockController(Models.StockContext context, IHttpClientFactory httpClientFactory)
      {
          _httpClientFactory = httpClientFactory;
          _context = context;
      }
  
      // POST api/values
      [HttpPost]
      public async Task&lt;IActionResult&gt; Post([FromBody]Models.Stock stock)
      {
          _context.Stocks.Add(stock);
          await _context.SaveChangesAsync();
          return Ok(stock);
      }
  </code></pre>
  
  <p>This is the startup.cs code:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
      {
          services.AddCors(o =&gt; o.AddPolicy("MyPolicy", corsBuilder =&gt;
          {
              corsBuilder.AllowAnyOrigin()
                  .AllowAnyMethod()
                  .AllowAnyHeader()
                  .AllowCredentials();
          }));
  
          services.AddDbContext&lt;DataContext&gt;(x =&gt; x.UseInMemoryDatabase("TestDb"));
          services.AddHttpClient();
          services.AddAutoMapper();
  
          // configure strongly typed settings objects
          var appSettingsSection = Configuration.GetSection("AppSettings");
          services.Configure&lt;AppSettings&gt;(appSettingsSection);
  
          // configure jwt authentication
          var appSettings = appSettingsSection.Get&lt;AppSettings&gt;();
          var key = Encoding.ASCII.GetBytes(appSettings.Secret);
          services.AddAuthentication(x =&gt;
          {
              x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
              x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
          })
          .AddJwtBearer(x =&gt;
          {
              x.Events = new JwtBearerEvents
              {
                  OnTokenValidated = context =&gt;
                  {
                      var userService = context.HttpContext.RequestServices.GetRequiredService&lt;IUserService&gt;();
                      var userId = int.Parse(context.Principal.Identity.Name);
                      var user = userService.GetById(userId);
                      if (user == null)
                      {
                          // return unauthorized if user no longer exists
                          context.Fail("Unauthorized");
                      }
                      return Task.CompletedTask;
                  }
              };
              x.RequireHttpsMetadata = false;
              x.SaveToken = true;
              x.TokenValidationParameters = new TokenValidationParameters
              {
                  ValidateIssuerSigningKey = true,
                  IssuerSigningKey = new SymmetricSecurityKey(key),
                  ValidateIssuer = false,
                  ValidateAudience = false
              };
          });
  
          // configure DI for application services
          services.AddScoped&lt;IUserService, UserService&gt;();
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
  
      }
  </code></pre>
  
  <p>This worked before I added the registration, login and </p>
  
  <p>// configure DI for application services
              services.AddScoped();</p>