IOptions 的 .NET Core 3 Worker 服务依赖注入配置 [英] .NET Core 3 Worker Service Dependency Injection Configuration by IOptions

查看:21
本文介绍了IOptions 的 .NET Core 3 Worker 服务依赖注入配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Worker Service 的 DI 问题,该问题在下面的另一篇文章中得到了解答.

i have a question about DI on Worker Service that answered another post below.

.NET Core 3 Worker 服务设置依赖注入

如果我想添加一些帮助类并按如下方式注册怎么办.我如何使用该选项注入.因为我想,我错过了一些东西......

what if i want to add some helper class and registered as below. How can i use that option injection. because i think, i missed something...

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 这样的构造函数.

RedisHelper class has a constructor like this as Worker.

public static MySettings _configuration { get; set; }

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

推荐答案

无需自己构建配置.您可以通过 hostContext

No need to build the config yourself. You can access it in the ConfigureServices via the hostContext

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>(); 
        });
}

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

Now it is just a matter of injecting the options into the desired helper class

//...

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

//...

和 Worker 服务

and the Worker service

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

这篇关于IOptions 的 .NET Core 3 Worker 服务依赖注入配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆