title: ASP.Net Core MVC Dependency Injection not working
body: <p>I am trying to inject a interface into to my HomeController and I am getting this error:</p>
  
  <blockquote>
    <p>InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration' while attempting to activate </p>
  </blockquote>
  
  <p>My Startup class is as follows:</p>
  
  <pre><code>public Startup(IApplicationEnvironment appEnv)
  {
      var builder = new ConfigurationBuilder()
          .SetBasePath(appEnv.ApplicationBasePath)
          .AddEnvironmentVariables()
          .AddJsonFile("appsettings.json");
      Configuration = builder.Build();
  }
  
  public IConfigurationRoot Configuration { get; set; }
  
  public void ConfigureServices(IServiceCollection services)
  {
      // Add framework services.
      services.AddApplicationInsightsTelemetry(Configuration);
  
      services.AddEntityFramework()
          .AddSqlServer()
          .AddDbContext&lt;ApplicationDbContext&gt;(options =&gt; options
              .UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
  
      services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()
          .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
          .AddDefaultTokenProviders();
  
      services.AddMvc();
      services.AddSingleton(provider =&gt; Configuration);
  
      // Add application services.
      services.AddTransient&lt;IEmailSender, AuthMessageSender&gt;();
      services.AddTransient&lt;ISmsSender, AuthMessageSender&gt;();
  }
  
  public void Configure(
      IApplicationBuilder app, 
      IHostingEnvironment env, 
      ILoggerFactory loggerFactory)
  {
      loggerFactory.AddConsole(Configuration.GetSection("Logging"));
      loggerFactory.AddDebug();
  
      app.UseApplicationInsightsRequestTelemetry();
  
      if (env.IsDevelopment())
      {
          app.UseBrowserLink();
          app.UseDeveloperExceptionPage();
          app.UseDatabaseErrorPage();
      }
      else
      {
          app.UseExceptionHandler("/Home/Error");
  
          try
          {
              using (var serviceScope = app.ApplicationServices
                  .GetRequiredService&lt;IServiceScopeFactory&gt;()
                  .CreateScope())
              {
                  serviceScope.ServiceProvider
                          .GetService&lt;ApplicationDbContext&gt;()
                          .Database.Migrate();
              }
          }
          catch { }
      }
  
      app.UseIISPlatformHandler(options =&gt; options.AuthenticationDescriptions.Clear());
  
      app.UseApplicationInsightsExceptionTelemetry();
      app.UseStaticFiles();
      app.UseIdentity();
  
      app.UseMvc(routes =&gt;
      {
          routes.MapRoute(
              name: "default",
              template: "{controller=Home}/{action=Index}/{id?}");
      });
  
      app.Run((async (context) =&gt;
      {
          await context.Response.WriteAsync("Error");
      }));
  }
  </code></pre>
  
  <p>and my HomeController constructor is:</p>
  
  <pre><code>public HomeController(IConfiguration configuration, IEmailSender mailService)
  {
      _mailService = mailService;
      _to = configuration["emailAddress.Support"];
  }
  </code></pre>
  
  <p>Please tell me where I am mistaken.</p>
  
  <blockquote>
    <p>Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider provider, ISet`1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)</p>
  </blockquote>