.net-coreconfigurationrabbitmqmasstransittopshelf

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


I'm trying to develop/convert current .Net framework code of Task Consumer to Top shelf and .NET core 3.1 . I want to read the config data from json file and use those in start as well as in consume methods.

What is the best and simplest way to pass config data to Task Consumer service and other end points

Any suggestion/comments are helpful.

Thanks in advance .

My current code is

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

            });

            }
        }

Consumer code

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

          }

Solution

  • I'd recommend moving from Topshelf to the .NET Core Generic Host, which is used in the console service sample. It uses the standard .NET Core extension packages for configuration, dependency injection, etc.