简体   繁体   English

.NET 6(稳定)Program.cs 中的 IConfiguration 设置

[英].NET 6 (stable) IConfiguration setup in Program.cs

This appears to be a similar problem but none of the answers are fitting for my code...: Read appsettings.json in Main Program.cs这似乎是一个类似的问题,但没有一个答案适合我的代码......: 阅读 Main Program.cs 中的 appsettings.json

This is extremely similar but does not work for me for some reason: ASP.NET Core 6 how to access Configuration during setup这非常相似,但由于某种原因对我不起作用: ASP.NET Core 6 how to access Configuration during setup

I'm porting a .NET 6 beta application to stable version of .NET 6.我正在将 .NET 6 beta 应用程序移植到 .NET 6 的稳定版本。

This means I want to port the code responsible for importing IConfiguration (yes I'm aware it's backwards compatible with the separate Startup.cs and Program.cs files, but I'm willing to use the new syntax).这意味着我想移植负责导入 IConfiguration 的代码(是的,我知道它向后兼容单独的Startup.csProgram.cs文件,但我愿意使用新语法)。 IConfiguration is basically the file appsettings.json in ASP.NET Core. IConfiguration 基本上就是 ASP.NET Core 中的文件 appsettings.json。

In old code it looks like this:在旧代码中,它看起来像这样:

 public class Startup
    {
        internal static IConfiguration Configuration { get; private set; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

I tried the following:我尝试了以下方法:

using Microsoft.Extensions.Configuration;
// IConfiguration init test
var Configuration = builder.Configuration;

It seems that the Microsoft.Extensions.Configuration isn't even used: The syntax I got is from this blog post: https://gavilan.blog/2021/08/19/getting-the-iconfiguration-in-the-program-class-asp-net-core-6/似乎甚至没有使用 Microsoft.Extensions.Configuration:我得到的语法来自这篇博客文章: https://gavilan.blog/2021/08/19/getting-the-iconfiguration-in-the-program -class-asp-net-core-6/

This problem is primitive, but I don't seem to get it right.这个问题很原始,但我似乎没有做对。 Any help is appreciated to solve this problem being that it works in new .NET 6 minimal API.感谢任何帮助解决此问题,因为它适用于新的 .NET 6 最小 API。

I found this guide useful but it apparently didn't solve the problem at hand: https://www.mikesdotnetting.com/article/357/razor-pages-startup-in-net-6我发现本指南很有用,但显然没有解决手头的问题: https://www.mikesdotnetting.com/article/357/razor-pages-startup-in-net-6

In another Model I have this code which is also not valid anymore:在另一个 Model 我有这个代码也不再有效:

secret = Startup.Configuration["GoogleRecaptchaV3:Secret"];
                path = Startup.Configuration["GoogleRecaptchaV3:ApiUrl"];

I tried replacing Startup with Program but it didn't seem to work...我尝试用 Program 替换 Startup 但它似乎没有用......

The errors I'm getting are:我得到的错误是:

'Program' does not contain a definition for 'Configuration'

The following works as a basic and direct implementation:以下是基本和直接的实现:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var config = app.Configuration;

Edited following Guru Stron's comment.根据 Guru Stron 的评论进行编辑。

  1. Switching to minimal hosting model is not required, you can still use the generic hosting (the one with startup)不需要切换到最小主机 model ,您仍然可以使用通用主机(带启动的主机)

It seems that the Microsoft.Extensions.Configuration isn't even used!似乎 Microsoft.Extensions.Configuration 甚至没有使用!

The using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration; directive is not needed cause new feature global using statements heavily used in .NET 6 templates不需要指令导致新功能全局 using 语句在 .NET 6 模板中大量使用

3. 3.

var Configuration = builder.Configuration; var Configuration = builder.Configuration;

The Program file in new templates is using another relatively new feature (C# 9) - top-level statements , so the var Configuration is just an variable accessible only inside the generated Main method新模板中的Program文件正在使用另一个相对较新的功能 (C# 9) - 顶级语句,因此var Configuration只是一个只能在生成的Main方法中访问的变量

4. 4.

secret = Startup.Configuration["GoogleRecaptchaV3:Secret"]; secret = Startup.Configuration["GoogleRecaptchaV3:Secret"];

You can access this value inside the top-level statement file via:您可以通过以下方式在顶级语句文件中访问此值:

var secret = builder.Configuration["GoogleRecaptchaV3:Secret"];

To make settings accessible outside that file (or Startup in generic hosting) I highly recommend to use DI (as shown in the docs here or here ).为了使设置可以在该文件之外访问(或通用托管中的Startup ),我强烈建议使用 DI(如此处或此处文档中所示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM