Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 679
Paulo Henrique PH Avatar asked May 22 '17 07:05

Paulo Henrique PH


3 Answers

Use IConfiguration instead of IConfigurationIRoot

like image 166
Nomair Ghanem Avatar answered Nov 11 '22 12:11

Nomair Ghanem


cast it like below:

public Startup(IConfiguration configuration)
{
     Configuration = (IConfigurationRoot) configuration;
}
like image 40
Payam Khaninejad Avatar answered Nov 11 '22 12:11

Payam Khaninejad


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);
like image 28
InkHeart Avatar answered Nov 11 '22 13:11

InkHeart