gpt4 book ai didi

c# - "InvalidOperationException: No service for type ' Microsoft.Extensions.Configuration.IConfiguration ' has been registered."

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:26 25 4
gpt4 key购买 nike

我是编程的新手,也是 C# 和 .NET Core 的新手。

我在尝试确定为什么这个简单的“hello world”应用程序因我的标题错误而失败时遇到了一些困难。

我正在尝试让应用程序读取“Hello!!”来自 appsettings.json 并将其显示在屏幕上。

这是我的 Startup.cs

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

这是我的 Program.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();
}
}
}

这是我的 appsettings.json:

{
"Greeting": "Hello!!",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}

非常感谢您的帮助!

最佳答案

出现此问题是因为无法解析 IConfiguration 实例。您可以通过 ConfigurationBuilder 在 Startup 构造函数中定义实例这让您可以定义应该从何处获取配置值。

这是一个基本示例,其中 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);
});
}
}

关于c# - "InvalidOperationException: No service for type ' Microsoft.Extensions.Configuration.IConfiguration ' has been registered.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53015700/

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