.NET 5 到 .NET 6 的迁移 - 如何修复 appsettings.json 数据在迁移后被读取为 NULL?依赖注入不起作用

.NET 5 to .NET 6 migration - How to fix appsettings.json data being read as NULL after migration? Dependency injection not working

在 .NET 5 中,我们有一些设置是从 appsettings.json 文件中读取的。现在我们已经将代码从 .NET 5 迁移到 .NET 6,我们创建的用于保存应用程序设置的模型显示为空。所有代码都在构建并且 运行 没有错误,直到我们在读取 appsettings.json 文件时出现空错误。

.NET 5 一切正常。我们更新了 Program.cs 文件,删除了 Startup.cs 文件,更新了项目文件,并更新了所有 Nuget 包。怀疑是Program.cs文件中的服务配置有问题,用于设置检索,但我们还没有找到解决办法。

Program.cs 文件:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<IAPPRepository, APPRepository>();
builder.Services.AddHttpContextAccessor();
builder.Services.Configure<AppSettings>(options => builder.Configuration.GetSection("APPSettings"));


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

AppSettings.cs模型文件:

namespace APP_2
{
    public class AppSettings
    {
        public List<string> Uics { get; set; }
        public List<string> InvoiceTypes { get; set; }
        public List<string> InvoiceEntityTypes { get; set; }
        public List<Option> PeriodOptions { get; set; }
        public List<Option> MonthOptions { get; set; }
        public List<string> ConnectionStrings { get; set; }

    }
}

PageForm.cs模型文件:

namespace APP_2.Pages.FormModels
{
    public class InvoiceSelectionForm
    {
        private readonly IOptions<APPSettings> _APPSettings;
        public InvoiceSelectionForm(IOptions<APPSettings> APPSettings)
        {
            _APPSettings = APPSettings;
            InvoiceTypes = _APPSettings.Value.InvoiceTypes;
            InvoiceEntityTypes = _APPSettings.Value.InvoiceEntityTypes;
            MonthOptions = _APPSettings.Value.MonthOptions;
            PeriodOptions = _APPSettings.Value.PeriodOptions;
        }

        public string InvoiceEntityType { get; set; }
        public string InvoiceType { get; set; }
        public string FiscalYear { get; set; }
        public bool UsePeriod { get; set; }
        public string FiscalPeriod { get; set; }
        public List<string> InvoiceTypes { get; set; }
        public List<string> InvoiceEntityTypes { get; set; }
        public List<Option> PeriodOptions { get; set; }
        public List<Option> MonthOptions { get; set; }
        public List<string> FiscalYears { get; set; } = Enumerable.Range(2012, DateTime.Now.Year - 2010).Reverse().Select(n => n.ToString()).ToList();
        public List<Option> SelectedInvoiceEntityTypes { get; set; } = new List<Option>();
        public List<Option> AvailableInvoiceEntityTypes { get; set; } = new List<Option>();

    }
}

PageCode.cs 文件:

namespace APP_2.Pages
{
    public partial class InvoiceSelection
    {        

        private IOptions<APPSettings> APPSettings { get; set; }

        protected override void OnInitialized()
        {
            form = new InvoiceSelectionForm(APPSettings);
 
        }
}

正在调用错误的重载来配置此处的选项

//...
builder.Services.Configure<AppSettings>(options => builder.Configuration.GetSection("APPSettings"));
//...

直接传递部分而不是配置委托

//...

builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("APPSettings"));

//...

这应该可以解决 PageForm.cs 选项被正确注入的问题。