尝试激活'TheWorld.Startup'时无法解析类型'Microsoft.Extensions.Configuration.IConfigurationRoot'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试激活'TheWorld.Startup'时无法解析类型'Microsoft.Extensions.Configuration.IConfigurationRoot'相关的知识,希望对你有一定的参考价值。

好像我有一些不匹配的引用问题(对不起,我是ASP版本的新手)。

我使用VS 2016从头开始创建一个ASP.core应用程序。我的启动课程如下:

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" }
                );
             });
        }
    }

我虽然很难使用'IConfigurationRoot'。我通过IDE添加了它的引用,project.json看起来像这样:

{
  "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%" ]
  }

你们可以在这里发现任何愚蠢的错误吗?

干杯

答案

使用IConfiguration而不是IConfigurationIRoot

另一答案

尝试修改该行.SetBasePath(env.ContentRootPath)而不是.SetBasePath(_env.ContentRootPath)....这是由Visual Studio搭建的代码???

另一答案

像这样投射它来获取IConfigurationRoot

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

以上是关于尝试激活'TheWorld.Startup'时无法解析类型'Microsoft.Extensions.Configuration.IConfigurationRoot'的主要内容,如果未能解决你的问题,请参考以下文章

如何创建唯一的激活码?

Laravel - 尝试创建帐户,登录并发送激活电子邮件正在抛出“尝试获取非对象的属性”

需要激活一个窗口

我无法在 localhost 上激活我的 WordPress 插件

激活系统菜单

MyEclipse 2015激活之后过一会就报错,然后退出了,求助。。。