Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfigurationRoot' while attempting to activate 'TheWorld.Startup'

unable to resolve service for type while attempting to activate dbcontext
unable to resolve service for type while attempting to activate azure function
kestrel unable to resolve service for type
unable to resolve service for type context
unable to resolve service for type 'automapper.imapper' while attempting to activate
unable to resolve service for type 'system.net.http.httpclient' while attempting to activate
unable to resolve service for type 'microsoft extensions fileproviders ifileprovider
unable to resolve service for type 'microsoft extensions diagnostics healthchecks healthcheckservice

Seems like I'm having some sort of mismatching reference problem (Sorry, Im very new on the ASP version).

Im using VS 2016 creating an ASP.core app from scratch. My startup class goes like:

public class Startup
    {
        private IHostingEnvironment _env;
        private IConfigurationRoot _config;

        public Startup(IHostingEnvironment env, IConfigurationRoot config)
        {
            _env = env;

            var builder = new ConfigurationBuilder()
                .SetBasePath(_env.ContentRootPath)
                .AddJsonFile("config.json");

            _config = builder.Build();
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(_config);

            if(_env.IsEnvironment("Development"))
               services.AddScoped<Services.IMailServices, Services.DebugMailService>();

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {

            if(env.IsDevelopment())
               app.UseDeveloperExceptionPage();

            app.UseStaticFiles();
            app.UseMvc(config =>
            {
                config.MapRoute(name: "Default",
                template: "{controller}/{action}/{id?}",
                defaults: new { controller = "App", action = "Index" }
                );
             });
        }
    }

Im having a hard time with the 'IConfigurationRoot' though. I've added its reference via IDE, and the project.json looks like this:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.1.3",
    "Microsoft.AspNetCore.Mvc.TagHelpers": "1.1.3",
    "Microsoft.Extensions.Configuration.FileExtensions":  "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }

You guys can spot any silly mistake(s) in here?

Cheers

Use IConfiguration instead of IConfigurationIRoot

Unable to resolve service for type while attempting to activate, You need to register IRepository with the Dependency Injection framework. For example, in ConfigureServices , add the following: services. Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration' while attempting to activate. Resolution: Put this line in Startup.cs file under Configuration Services Method to inject dependency as shown below: services.AddSingleton<IConfiguration>(Configuration); // This method gets called by the runtime.

cast it like below:

public Startup(IConfiguration configuration)
{
     Configuration = (IConfigurationRoot) configuration;
}

ASP.NET Core: Error – InvalidOperationException Unable to resolve , Controllers, services, adapters, and repositories are all examples of InvalidOperationException: Unable to resolve service for type 'Tweet. InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' 8 Unable to find Use.RunTimePageInfo() method in startup.cs file in aspnet core

Try to modify that line .SetBasePath(env.ContentRootPath) instead of .SetBasePath(_env.ContentRootPath) .... Is this the code scaffolded by Visual Studio ???

Dependency Injection and Controllers, By simply adding a service type to your controller as a constructor parameter, ASP. InvalidOperationException: Unable to resolve service for type '​ControllerDI. Got error Unable to resolve service for type 'Microsoft.Extensions.Logging.ILoggerFactory' when using public Startup (IConfiguration configuration, ILoggerFactory loggerFactory)

The IConfigurationRoot is an interface which inherits from IConfiguration as below:

interface IConfigurationRoot : IConfiguration

So you need to use IConfiguration as the type specified in TServic.

Service type should be IConfiguration type and not IConfigurationRoot.

To add access to configuration, replace the below line:

services.AddSingleton(_config);

With:

services.AddSingleton<IConfiguration>(_config);

InvalidOperationException: Unable to resolve service for type · Issue , I'm using Core 2.1 razor pages with repository. Showing error while opening register page even I have registered Email sender in Startup.cs. InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' 84 Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthController'

Dependency Injection issue: Unable to resolve service for type , Dependency Injection issue: Unable to resolve service for type while attempting to activate [TimerFunction] #4939. Closed. adamgd opened  InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[IdentityUser]' because you used ApplicationUser in startup, not IdentityUser so this type is not registered with the injection system.

Unable to resolve service for type while attempting to activate , Hello , I just started learning ASP.NET Core , and I had this type of exception , and now im seeking for your help ! -Exception  System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' I've tried the solutions here but it didn't work for me. Edit Based on Yaakov's comment below and this Github comment I'm able to resolve it correctly by doing this. public MyClass(ILogger<MyClass> logger) { _logger = logger; }

Unable to resolve service for type context while attempting to , In this video , I am going to show you, If you pull the data from context in controller then must to Duration: 1:15 Posted: Jun 6, 2019 Unable to resolve service for type 'MediatR.IMediator' Ask Question Asked 1 month ago. Active 1 month ago. Viewed 96 times 0. I try to make .NET Core API with CQRS

Comments
  • Possible duplicate of ASP.Net Core MVC Dependency Injection not working