【问题标题】:.NET Core 3 Worker Service Dependency Injection Configuration by IOptions.NET Core 3 Worker 服务依赖注入配置通过 IOptions
【发布时间】:2019-10-30 00:52:32
【问题描述】:

我有一个关于 Worker Service 上的 DI 的问题,该问题回答了下面的另一篇文章。

.NET Core 3 Worker Service Settings Dependency Injection

如果我想添加一些帮助类并注册如下。 我如何使用该选项注入。 因为我想,我错过了什么……

public static IHostBuilder CreateHostBuilder(string[] args)
    {
        return Host.CreateDefaultBuilder(args)

            .ConfigureAppConfiguration((hostContext, config) =>
            {
                // Configure the app here.
                config
                .SetBasePath(Environment.CurrentDirectory)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);

                config.AddEnvironmentVariables();

                Configuration = config.Build();

            })

            .ConfigureServices((hostContext, services) =>
            {

                services.AddOptions();               

                services.Configure<MySettings>(Configuration.GetSection("MySettings"));                    


                services.AddSingleton<RedisHelper>();

                services.AddHostedService<Worker>();                   

            });
    }

RedisHelper 类有一个像这样的构造函数作为 Worker。

public static MySettings _configuration { get; set; }

public RedisHelper(IOptions<MySettings> configuration)
{
    if (configuration != null)
        _configuration = configuration.Value;
}

【问题讨论】:

    标签: c# dependency-injection .net-core backgroundworker


    【解决方案1】:

    无需自己构建配置。您可以通过 hostContext 在 ConfigureServices 中访问它

    public static IHostBuilder CreateHostBuilder(string[] args) {
        return Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostContext, config) => {
                // Configure the app here.
                config
                    .SetBasePath(Environment.CurrentDirectory)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
    
                config.AddEnvironmentVariables();
            })
            .ConfigureServices((hostContext, services) => {
    
                services.AddOptions();               
    
                services.Configure<MySettings>(hostContext.Configuration.GetSection("MySettings"));
                services.AddSingleton<RedisHelper>();
                services.AddHostedService<Worker>(); 
            });
    }
    

    现在只需将选项注入所需的辅助类

    //...
    
    public RedisHelper(IOptions<MySettings> configuration) {
        if (configuration != null)
            _configuration = configuration.Value;
    }
    
    //...
    

    和 Worker 服务

    public Worker(RedisHelper helper) {
        this.helper = helper;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-05
      • 1970-01-01
      • 2020-09-15
      • 2022-12-20
      • 1970-01-01
      • 2021-11-28
      • 2020-11-19
      • 1970-01-01
      • 2016-05-22
      相关资源
      最近更新 更多