/// <summary>
        /// The get request config.
        /// </summary>
        /// <param name="endpointName">
        /// The endpoint name.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The <see cref="IRequestConfiguration"/>.
        /// </returns>
        public IRequestConfiguration GetRequestConfig(string endpointName, string key)
        {
            EndpointElement endpoint = this.GetEndPointByName(endpointName);

            OutgoingElement reqDeclaration = endpoint.Outgoing.Cast <OutgoingElement>().
                                             First(x => x.Key == key);

            return(new RequestConfiguration(reqDeclaration.Timeout, reqDeclaration.Persist, reqDeclaration.Ttl));
        }
        /// <summary>
        /// The get event.
        /// </summary>
        /// <param name="endpointName">
        /// The endpoint name.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetEvent(string endpointName, string key)
        {
            EndpointElement endpoint = this.GetEndPointByName(endpointName);
            IEnumerable <MessageElement> messages = endpoint.Outgoing.Cast <MessageElement>().
                                                    Concat(endpoint.Incoming.Cast <MessageElement>());

            // NOTE: Если такого не будет, упадет соответствующий эксэпшн.
            return(messages.First(x => x.Key == key).
                   Label);
        }
        /// <summary>
        /// The get end point by name.
        /// </summary>
        /// <param name="endpointName">
        /// The endpoint name.
        /// </param>
        /// <returns>
        /// The <see cref="EndpointElement"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// </exception>
        private EndpointElement GetEndPointByName(string endpointName)
        {
            EndpointElement endpoint = this.endpointsConfig.Endpoints[endpointName];

            if (endpoint == null)
            {
                throw new ArgumentException(string.Format("Попытка найти конфигурацию для endpoint {0} закончилось провалом, пожалуйста укажите необходимую информацию в конфигурации {1}", endpointName, ServiceBusSectionName));
            }

            return(endpoint);
        }
        /// <summary>
        /// Конфигурирует клиента шины сообщений.
        /// </summary>
        /// <param name="endpointName">
        /// Имя точки подключения к шине.
        /// </param>
        /// <param name="cfg">
        /// Конфигуратор клиента шины.
        /// </param>
        /// <returns>
        /// Конфигуратор клиента шины, после применения к нему всех настроек.
        /// </returns>
        public IBusConfigurator Configure(string endpointName, IBusConfigurator cfg)
        {
            if (cfg == null)
            {
                throw new ArgumentNullException("cfg", "Файл конфигурации не может быть null");
            }

            EndpointElement endpointConfig = this.GetEndPointByName(endpointName);

            cfg.SetEndpoint(endpointConfig.Name);

            cfg.SetConnectionString(endpointConfig.ConnectionString);

            if (!string.IsNullOrWhiteSpace(endpointConfig.LifecycleHandler))
            {
                cfg.HandleLifecycleWith(this.ResolveLifecycleHandler(endpointConfig.LifecycleHandler));
            }

            if (endpointConfig.Caching != null && endpointConfig.Caching.Enabled)
            {
                cfg.EnableCaching();
            }

            if (endpointConfig.ParallelismLevel.HasValue)
            {
                cfg.UseParallelismLevel(endpointConfig.ParallelismLevel.Value);
            }

            if (endpointConfig.Dynamic != null)
            {
                if (endpointConfig.Dynamic.Outgoing.HasValue)
                {
                    if (endpointConfig.Dynamic.Outgoing.Value)
                    {
                        cfg.Route(MessageLabel.Any)
                        .ConfiguredWith(builder => new LambdaRouteResolver(
                                            (endpoint, label) =>
                        {
                            builder.Topology.Declare(
                                Exchange.Named(label.Name)
                                .Durable.Fanout);
                            return(new RabbitRoute(label.Name));
                        }));
                    }
                }
            }

            if (endpointConfig.Qos != null)
            {
                if (endpointConfig.Qos.PrefetchCount.HasValue)
                {
                    cfg.SetDefaultQoS(endpointConfig.Qos.PrefetchCount.Value);
                }
            }

            foreach (ValidatorElement validator in endpointConfig.Validators)
            {
                if (validator.Group)
                {
                    MessageValidatorGroup v = this.ResolveValidatorGroup(validator.Name);
                    cfg.RegisterValidators(v);
                }
                else
                {
                    IMessageValidator v = this.ResolveValidator(validator.Name);
                    cfg.RegisterValidator(v);
                }
            }

            foreach (OutgoingElement message in endpointConfig.Outgoing)
            {
                ISenderConfigurator senderCfg = cfg.Route(message.Label).
                                                WithAlias(message.Key);

                if (message.Confirm)
                {
                    senderCfg.WithConfirmation();
                }

                if (message.Persist)
                {
                    senderCfg.Persistently();
                }

                if (message.Ttl.HasValue)
                {
                    senderCfg.WithTtl(message.Ttl.Value);
                }

                if (message.CallbackEndpoint.Default)
                {
                    senderCfg.WithDefaultCallbackEndpoint();
                }

                if (message.Timeout.HasValue)
                {
                    senderCfg.WithRequestTimeout(message.Timeout);
                }
            }

            foreach (IncomingElement message in endpointConfig.Incoming)
            {
                IReceiverConfigurator receiver = cfg.On(message.Label).
                                                 WithAlias(message.Key);

                if (message.RequiresAccept)
                {
                    receiver.RequiresAccept();
                }

                Type messageType = typeof(ExpandoObject);
                if (!string.IsNullOrWhiteSpace(message.Type))
                {
                    messageType = ResolveType(message.Type);
                }

                var consumerFactory = this.BuildConsumerFactory(message.React, messageType);

                object consumer = BuildConsumer(consumerFactory, messageType, message.Lifestyle);

                RegisterConsumer(receiver, messageType, consumer);

                if (!string.IsNullOrWhiteSpace(message.Validate))
                {
                    IMessageValidator validator = this.ResolveValidator(message.Validate, messageType);

                    receiver.WhenVerifiedBy(validator);
                }
            }

            return(cfg);
        }