C# .NET 8 - Windows 服务 - 从不同项目中的 appsettings.json 获取值

问题描述 投票:0回答:1

我知道这个问题已经被问过数百次了,但我找到的所有解决方案都不适用于我的情况。也许我做的事情完全错误,所以我恳请有关此问题的任何帮助。

情况:

我想使用 .NET 8 创建 Windows 服务。 在我的解决方案中,我有 2 个项目 - 一个用于 GUI,称为

BrochureCreationService
(= 服务),一个用于应用程序逻辑的项目,称为
ApplicationLogic
:

现在我想访问我的

appsettings.json
文件,它当然位于 GUI 项目中 (
BrochureCreationService
)。 这是我的
appsettings.json
。我需要访问“AppSettings”部分:

{
  "AppSettings": {
    "Test": "test123"
  },
  ...
}

我创建了一个类

AppSettings
,其属性为
Test
:

namespace BrochureCreationService.ApplicationLogic.Models
{
    public class AppSettings
    {
        public string? Test = string.Empty;
    }
}

正如我所说,我已经用谷歌搜索/阅读了很多内容,但目前没有任何效果。

在我的

Program.cs
文件中,我想为
appsettings.json
文件提供此类 (
AppSettings
),该类应该能够注入到
ApplicationLogic
项目中。 我需要如何在
Program.cs
类中执行此操作?

我已经发现,我可以通过这种方式访问

appsettings.json

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

这就是我的

Program.cs
的样子:

using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.EventLog;
using BrochureCreationService.ApplicationLogic.Controllers;
using BrochureCreationService.GUI;
using BrochureCreationService.ApplicationLogic;
using BrochureCreationService.ApplicationLogic.Models;
using Microsoft.Extensions.Options;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWindowsService(options =>
{
    options.ServiceName = "Brochure Creation Service";
});

LoggerProviderOptions.RegisterProviderOptions<EventLogSettings, EventLogLoggerProvider>(builder.Services);

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

builder.Services.AddTransient<AppSettings>();
builder.Services.AddTransient<IServiceController, ServiceController>();

builder.Services.AddHostedService<WindowsBackgroundService>();

IHost host = builder.Build();
host.Run();

但是当我在项目

AppSettings
中注入
ApplicationLogic
类时,即在类
ServiceController.cs
中,属性
Test
的值为
string.Empty

namespace BrochureCreationService.ApplicationLogic.Controllers
{
    public class ServiceController : IServiceController
    {
        AppSettings _AppSettings;

        public ServiceController(AppSettings appSettings) 
        {
            _AppSettings = appSettings;
        }

        public void RunService()
        {
            var test = _AppSettings.Test;
        }
    }
}

为什么这不能以这种方式工作,或者我需要采取什么不同的措施才能使其工作? 预先非常感谢,再次抱歉再次提出此类问题。

c# dependency-injection appsettings .net-8.0
1个回答
0
投票

使用选项模式

不需要

builder.Services.AddTransient<AppSettings>();
builder.Services.AddOptions();

您还可以将 AppSettings 类中的部分名称定义为常量字符串。

public const string SectionName = "AppSettings";
然后在
Program.cs
中引用它,如下所示:
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection(AppSettings.SectionName)

另请参阅文档,使用 AddOptions 模式。 IE。代替:

builder.Services.Configure<AppSettings>(builder.Configuration.GetSection(AppSettings.SectionName)

builder.Services.AddOptions<AppSettings>().BindConfiguration(AppSettings.SectionName);

public class ServiceController : IServiceController
    {
        AppSettings _AppSettings;
        public ServiceController(IOptions<AppSettings> appSettings) 
        {
            _AppSettings = appSettings.Value;
        }
        public void RunService()
        {
            var test = _AppSettings.Test;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.