gpt4 book ai didi

c# - 尝试激活 'Microsoft.Extensions.Configuration.IConfigurationRoot' 时无法解析类型 'TheWorld.Startup' 的服务

转载 作者:行者123 更新时间:2023-11-30 14:23:45 26 4
gpt4 key购买 nike

似乎我遇到了某种不匹配的引用问题(抱歉,我对 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

关于c# - 尝试激活 'Microsoft.Extensions.Configuration.IConfigurationRoot' 时无法解析类型 'TheWorld.Startup' 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44106765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com