namespace RequestService
{
    using System;
    using System.Configuration;
    using MassTransit;
    using MassTransit.RabbitMqTransport;
    using MassTransit.Util;
    using Topshelf;
    using Topshelf.Logging;
    using ServiceController;
    //using GreenPipes;

    class RequestService :   ServiceControl
    {
        readonly LogWriter _log = HostLogger.Get<RequestService>();

        //IBusControl _busControl;
        //[Obsolete]
        public bool Start(HostControl hostControl)
        {
            try
            {
                _log.Info("Creating bus...");
                Global._busControl = Bus.Factory.CreateUsingRabbitMq(x =>
                {
                    var host = x.Host(new Uri(Global.RabbitMQHost), h =>
                    {
                        h.Username(Global.username);
                        h.Password(Global.password);
                    });
                    x.ReceiveEndpoint(Global.ServiceQueueName,
                        e =>
                        {
                            e.PrefetchCount = 5;
                        //e.UseConcurrencyLimit(5);
                        e.Consumer<RequestConsumer>();
                        });
                });
                _log.Info("Starting bus...");
                TaskUtil.Await(() => Global._busControl.StartAsync());               
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return true;
        }
        public bool Stop(HostControl hostControl)
        {
            _log.Info("Stopping bus...");
            Global._busControl?.Stop();
            return true;
        }

    }
}
