在 .Net 核心中将配置传递给 Topshelf.HostFactory.Run 的最佳方式

Best way to pass configuration to Topshelf.HostFactory.Run in .Net core

我正在尝试 develop/convert Task Consumer 的当前 .Net 框架代码到 Top shelf 和 .NET core 3.1。我想从 json 文件中读取配置数据并在启动和使用方法中使用它们。

将配置数据传递到任务使用者服务和其他端点的最佳和最简单的方法是什么

任何 suggestion/comments 都有帮助。

提前致谢。

我当前的密码是

static void Main(string[] args)
            {

            var configuration  = new ConfigurationBuilder().AddJsonFile("appsettings.json", true,true).Build();
            var test2 = configuration.GetSection("RabbitMQSettings");
            Console.WriteLine(HostFactory.Run(cfg => cfg.Service<TaskConsumerService>()));
            }

ConsumerService code 
 public class TaskConsumerService : ServiceControl
        {
        IBusControl _bus;
        IConfiguration _configuration;

        public bool Start(HostControl hostControl)
            {

            _bus = ConfigureBus();
            _bus.Start();

            return true;
            }

        public bool Stop(HostControl hostControl)
            {
            _bus?.Stop(TimeSpan.FromSeconds(30));

            return true;
            }

        IBusControl ConfigureBus()
            {

            return Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                var rabbitMQUrl = ConfigurationManager.AppSettings["RabbitMQSettings:RabbitMQHostUrl"];
                cfg.Host(new Uri(rabbitMQUrl) , h =>
                {
                    h.Username(ConfigurationManager.AppSettings["RabbitMQSettings:username"]);
                    h.Password(ConfigurationManager.AppSettings["RabbitMQSettings:pwd"]);

                });

                var queue0 =  ConfigurationManager.AppSettings["QueName"];
                cfg.ReceiveEndpoint(queue0 , e =>
                {
                    e.Consumer<TransformConsumer>();
                });

            });

            }
        }

消费者代码

 public class TransformConsumer : IConsumer<IExecuteTransform>
        {

        private static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        string address = string.Empty;
        const string json = "application/json";

        public async Task Consume(ConsumeContext<IExecuteTransform> context)
            {
            m_log.Info(" Transform started.");
            // Processing
            try
                {
                 address = string.Concat(ConfigurationManager.AppSettings["RabbitMQSettings:RabbitMQHost"] , ConfigurationManager.AppSettings["RabbitMQSettings:queName"]);
                 IExecuteTransform message = await ConsumeSendEndPoint(context , address);
                 m_log.Info(" Transform Process completed.");
                }
            catch ( Exception e )
                {
                m_log.Error("Transform failed");
                throw e;
                }
            }

          }

我建议从 Topshelf 转移到 console service 示例中使用的 .NET Core 通用主机。它使用标准的 .NET Core 扩展包进行配置、依赖注入等。