示例#1
0
        private void EnsureTopicExists(string topic)
        {
            if (_topicCreated || _makeChannel.Equals(OnMissingChannel.Assume))
            {
                return;
            }

            try
            {
                if (_managementClientWrapper.TopicExists(topic))
                {
                    _topicCreated = true;
                    return;
                }

                if (_makeChannel.Equals(OnMissingChannel.Validate))
                {
                    throw new ChannelFailureException($"Topic {topic} does not exist and missing channel mode set to Validate.");
                }

                _managementClientWrapper.CreateTopic(topic);
                _topicCreated = true;
            }
            catch (Exception e)
            {
                //The connection to Azure Service bus may have failed so we re-establish the connection.
                _managementClientWrapper.Reset();
                s_logger.LogError(e, "Failing to check or create topic.");
                throw;
            }
        }
示例#2
0
        private void EnsureSubscription()
        {
            const int maxDeliveryCount = 2000;

            if (_subscriptionCreated || _makeChannel.Equals(OnMissingChannel.Assume))
            {
                return;
            }

            try
            {
                if (_managementClientWrapper.SubscriptionExists(_topicName, _subscriptionName))
                {
                    _subscriptionCreated = true;
                    return;
                }

                if (_makeChannel.Equals(OnMissingChannel.Validate))
                {
                    throw new ChannelFailureException($"Subscription {_subscriptionName} does not exist on topic {_topicName} and missing channel mode set to Validate.");
                }

                _managementClientWrapper.CreateSubscription(_topicName, _subscriptionName, maxDeliveryCount);
                _subscriptionCreated = true;
            }
            catch (MessagingEntityAlreadyExistsException)
            {
                s_logger.LogWarning("Message entity already exists with topic {Topic} and subscription {ChannelName}.", _topicName,
                                    _subscriptionName);
                _subscriptionCreated = true;
            }
            catch (Exception e)
            {
                s_logger.LogError(e, "Failing to check or create subscription.");

                //The connection to Azure Service bus may have failed so we re-establish the connection.
                _managementClientWrapper.Reset();

                throw new ChannelFailureException("Failing to check or create subscription", e);
            }
        }