通过工作人员发布到.net核心中的API发送多个appsettings.json值的简单方法?



我正在.Net Core 3.1中构建一个C# Windows Service API轮询应用程序。这将在服务器上 24/7 全天候运行,每 5 分钟轮询一次。

我想要一种更简单的方法,通过我的 API post 调用发送参数,直接从我的 appsettings.json 的主体发送值,而不是为我将轮询的每个对象创建新类和强类型。

我的应用程序当前使用一个给定以下代码的 appsettings 对象

appsettings.json

"ReceiptSettings": {
"select_topx": "100",
"tran_type": "REC",
"sub_type": "PO",
"max_age_min": "10000",
"integration_name": "ERP"
}

工人:

public IConfiguration _configuration { get; private set; }
...
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Timed Hosted Service running.");
_timer = new Timer(DoWork, Task.CompletedTask, TimeSpan.Zero,
TimeSpan.FromMinutes(5));
await Task.CompletedTask;
}
private async void DoWork(object state)
{
// Keep garbage collector from collecting and stopping timer
GC.KeepAlive(_timer);
// Don't let timer execute again if we are still processing last iteration
if (Monitor.TryEnter(_locker))
{
try
{
RunPoller();
}
finally
{
Monitor.Exit(_locker);
}
}
}
private async void RunPoller()
{
var count = Interlocked.Increment(ref executionCount);
// Do long running work here
_logger.LogInformation($"Polling started. Iteration: {count}");
await Task.Run(() => RunPoller.Run(_logger, _apiSettings, _configuration));
}

运行波器:

public static IConfiguration _iConfiguration { get; private set; }
public static async Task Run(ILogger<Worker> logger, IOptions<APIConfiguration> apiSettings, IConfiguration configuration)
{
...
try
{
Dictionary<string, object> settings = _config
.GetSection("ReceiptSettings")
.Get<Dictionary<string, object>>();
string json = JsonConvert.SerializeObject(_settings);
var payload = new StringContent(json, Encoding.UTF8, "application/json");
...
}
...

我想重构它以遍历 PollingSettings 对象,根据值为 PollingSettings 中的每个设置创建一个新线程

"PollingSettings": {
"ReceiptSettings": {
"select_topx": "100",
"tran_type": "REC",
"sub_type": "PO",
"max_age_min": "10000",
"integration_name": "ERP"
},
"InventorySettings": {
"select_topx": "100",
"tran_type": "IN",
"sub_type": "ADJ",
"mark_pickup_id": "1"
}

我已经有了强类型的 apiSettings,并且可以做到这一点。但是我必须在Visual Studio中的对象和类中保持更改。如果我通过此方法动态发送它们,那么我可以在记事本中打开 appsettings.json 文件并在更短的时间内修改参数。

我已经尝试过这个,但是我得到空值错误,并且第一次迭代没有处理 RunPoller 类:

private async void DoWork(object state)
{
// Keep garbage collector from collecting and stopping timer
GC.KeepAlive(_timer);
var pollingConfigs = Configuration.GetSection("PollingSettings").GetChildren();
// Don't let timer execute again if we are still processing last iteration
if (Monitor.TryEnter(_locker))
{
try
{
foreach (var setting in pollingConfigs)
{
Dictionary<string, object> settings = Configuration
.GetSection(setting.Key)
.Get<Dictionary<string, object>>();
RunPoller(settings);
}
}
finally
{
Monitor.Exit(_locker);
}
}
}
private async void RunPoller(Dictionary<string, object> settings)
{
// Do long running work here
_logger.LogInformation($"Polling started. Iteration: {count}");
await Task.Run(() => RunPoller.Run(_logger, _apiSettings, settings));
}

(将设置的 DI 保留到 RunPoller.Run,为简洁起见,删除了代码。

有什么方法可以正确做到这一点吗?

对于现有代码,您只需更改路径即可获取子部分,如以下代码:
1 - 通过Path更改Key.GetSection(setting.Key)

var pollingConfigs = Configuration.GetSection("PollingSettings").GetChildren();
// Don't let timer execute again if we are still processing last iteration
if (Monitor.TryEnter(_locker))
{
try
{
foreach (var setting in pollingConfigs)
{
Dictionary<string, object> settings = Configuration
.GetSection(setting.Path)
.Get<Dictionary<string, object>>();
RunPoller(settings);
}
}
finally
{
Monitor.Exit(_locker);
}
}

2 - 您也可以使用此解决方案:2.1 获取Dictionary<string, object>列表:

var pollingConfigs = _configuration.GetSection("PollingSettings")
.GetChildren()
.Select(y => y.Get<Dictionary<string, object>>());

2.2 在循环中,直接使用列表的值:

foreach (Dictionary<string, object> setting in pollingConfigs)
{
RunPoller(setting);
}

我希望对您有所帮助。

最新更新