private static AMQPConfig PrepareAMQPConfig(string connectionString, string hubName,
                                                    string messageSubject, string messageDeviceId, string messageDeviceDisplayName)
        {
            try
            {
                NamespaceManager    nsmgr = NamespaceManager.CreateFromConnectionString(connectionString);
                EventHubDescription desc  = nsmgr.GetEventHub(hubName);

                foreach (var rule in desc.Authorization)
                {
                    var accessAuthorizationRule = rule as SharedAccessAuthorizationRule;
                    if (accessAuthorizationRule == null)
                    {
                        continue;
                    }
                    if (!accessAuthorizationRule.Rights.Contains(AccessRights.Send))
                    {
                        continue;
                    }

                    string amqpAddress = string.Format("amqps://{0}:{1}@{2}",
                                                       accessAuthorizationRule.KeyName,
                                                       Uri.EscapeDataString(accessAuthorizationRule.PrimaryKey), nsmgr.Address.Host);

                    AMQPConfig amqpConfig = new AMQPConfig
                    {
                        AMQPSAddress = amqpAddress,
                        EventHubName = hubName,
                        EventHubDeviceDisplayName =
                            string.IsNullOrEmpty(messageSubject) ? "SensorGatewayService" : messageSubject,
                        EventHubDeviceId =
                            string.IsNullOrEmpty(messageDeviceId)
                                ? "a94cd58f-4698-4d6a-b9b5-4e3e0f794618"
                                : messageDeviceId,
                        EventHubMessageSubject =
                            string.IsNullOrEmpty(messageDeviceDisplayName) ? "gtsv" : messageDeviceDisplayName
                    };
                    return(amqpConfig);
                }
            }
            catch (Exception)
            {
            }
            return(null);
        }
        private static GatewayService CreateGateway(AMQPConfig amqpConfig)
        {
            try
            {
                var _gatewayQueue = new GatewayQueue <QueuedItem>();

                var _AMPQSender = new AMQPSender <string>(
                    amqpConfig.AMQPSAddress,
                    amqpConfig.EventHubName,
                    amqpConfig.EventHubMessageSubject,
                    amqpConfig.EventHubDeviceId,
                    amqpConfig.EventHubDeviceDisplayName,
                    _Logger
                    );

                var _batchSenderThread = new BatchSenderThread <QueuedItem, string>(
                    _gatewayQueue,
                    _AMPQSender,
                    null,
                    m => m.JsonData,
                    _Logger);

                _batchSenderThread.Start();

                GatewayService service = new GatewayService(
                    _gatewayQueue,
                    _batchSenderThread
                    )
                {
                    Logger = _Logger
                };

                service.OnDataInQueue += (data) => _batchSenderThread.Process();
                _Logger.Flush();

                return(service);
            }
            catch (Exception ex)
            {
                _Logger.LogError("Exception on creating Gateway: " + ex.Message);
            }

            return(null);
        }
        private static void InitWorkerConfiguration()
        {
            AppConfiguration config = Loader.GetConfig(_Logger);
            IEnumerable <RawXMLWithHeaderToJsonReader> readers = PrepareReaders(config.XmlTemplate, config.UseXml, config.CredentialToUse);

            AMQPConfig amqpDevicesConfig = PrepareAMQPConfig(config.ServiceBusConnectionString,
                                                             config.EventHubName,
                                                             config.MessageSubject,
                                                             config.MessageDeviceId,
                                                             config.MessageDeviceDisplayName);

            if (amqpDevicesConfig == null)
            {
                Interlocked.Exchange(ref _Gateway, null);
                Interlocked.Exchange(ref _Readers, null);
                Interlocked.Exchange(ref _Config, config);
                _Logger.LogInfo("Not able to construct AMQP config for Event Hub using provided connection string...");
                return;
            }
            Interlocked.Exchange(ref _Gateway, CreateGateway(amqpDevicesConfig));
            Interlocked.Exchange(ref _Readers, readers);
            Interlocked.Exchange(ref _Config, config);
        }