将配置传递给IHostedService

时间:2019-04-26 06:46:51

标签: c# .net-core

我正在尝试向%添加配置值,但不知道如何。

这是我目前所拥有的:

启动

CompletableFuture<BigDecimal> result = lotOfWork.reduce((a,b) -> a.thenCombine(b, BigDecimal::add)).orElse(CompletableFuture.completedFuture(BigDecimal.ZERO));

MyOptions

IHostedService

应用设置

services.Configure<MyOptions>(Configuration.GetSection("MyOptions"));
services.AddHostedService<MyHostedService>();

MyHostedService

public class MyOptions
{
    public string MyOption{ get; set; }
}

1 个答案:

答案 0 :(得分:1)

您快到了。您只剩下一件事要做:

只需将constructor dependency injectionIOptions<MyOptions>中的IHostedService或相关的IOptions<TOptions>一起使用:

public MyHostedService(ILogger<MyHostedService> logger, IOptions<MyOptions> optionsAccessor)
{
    _logger = logger;
    _options = optionsAccessor.Value;
}

有关更多详细信息,请参见body-parser

相关问题