C# &引用;InvalidOperationException:类型为';的服务不可用;Microsoft.Extensions.Configuration.IConfiguration';已注册。”;
我对编程有点陌生,对c#和.NET内核也很陌生 我很难确定为什么这个简单的“hello world”应用程序会因为标题中的错误而失败 我试图让应用程序读取appsettings.json中的“Hello!!”并将其显示在屏幕上 这是我的Startup.csC# &引用;InvalidOperationException:类型为';的服务不可用;Microsoft.Extensions.Configuration.IConfiguration';已注册。”;,c#,.net,asp.net-core,C#,.net,Asp.net Core,我对编程有点陌生,对c#和.NET内核也很陌生 我很难确定为什么这个简单的“hello world”应用程序会因为标题中的错误而失败 我试图让应用程序读取appsettings.json中的“Hello!!”并将其显示在屏幕上 这是我的Startup.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
namespace My_NET_Core_Test_2
{
public class Startup
{
// 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 https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// 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,
IConfiguration configuration)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
var greeting = configuration["Greeting"];
await context.Response.WriteAsync(greeting);
});
}
}
}
这是我的程序。cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
namespace My_NET_Core_Test_2
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
}
我感谢你的帮助 此问题可能是因为无法解决IConfiguration实例。您可以通过在启动构造函数中定义实例,让我们定义从何处获取配置值 下面是一个基本示例,其中ConfigurationBuilder将从内容根路径中的appsettings.json读取值,并读取环境变量
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
_configuration = 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 https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// 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)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
var greeting = _configuration["Greeting"];
await context.Response.WriteAsync(greeting);
});
}
}
也许我遗漏了什么,但是您在哪里注册
IConfiguration
的实现呢?看一看,看起来您使用的是旧版本的ASP.NET Core(如果不是,则使用的是旧方法)。您实际使用的是哪个版本?您的项目版本是什么?我在.net core 2.1下对您的代码进行了测试,我只需要通过`var host=new WebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).ConfigureAppConfiguration((hostingContext,config)=>{config.AddJsonFile(“appsettings.json”,可选:false,reloadOnChange:false);}).UseIISIntegration().UseStartup().Build();`Hmm.我已经尝试将项目的目标框架设置为.NET Core 2.1(在使用1.1之前),我得到了相同的结果。当我尝试更新我的Program.cs Main方法以使用Tao Zhou建议的代码时,Visual Studio似乎不喜欢ConfigureAppConfiguration行。“IWebHostBuilder不包含…的定义。”
public class Startup
{
private readonly IConfiguration _configuration;
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
_configuration = 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 https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// 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)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
var greeting = _configuration["Greeting"];
await context.Response.WriteAsync(greeting);
});
}
}