Beispiel #1
0
        private SubscriberInfo SubscribeOne(IServiceBusSubscriber <TMessage> subscriber)
        {
            string topicName;
            string subscriptionName;

            if (!TryGetSubscriberTopicSubscriptionNames(subscriber, out topicName, out subscriptionName))
            {
                throw new InvalidOperationException(string.Format(
                                                        "No subscriber topic/subscription attribute specified for type '{0}'", typeof(TMessage).FullName));
            }

            var serviceBusConfiguration = _configurationService.Get <AzureServiceBusConfiguration>();

            var subscriptionConfiguration = serviceBusConfiguration.Topics
                                            .Where(each => each.TopicName == topicName)
                                            .SelectMany(each => each.Subscriptions)
                                            .FirstOrDefault(each => each.SubscriptionName == subscriptionName);

            if (subscriptionConfiguration == null)
            {
                _logService.Info(
                    "No subscriber configuration found for topic/subscription \"{0}/{1}\", will use default configuration",
                    topicName, subscriptionName);

                subscriptionConfiguration = new AzureServiceBusSubscriptionConfiguration();
            }

            //Ensure topic name is correctly formatted after its configuration has been retrieved
            topicName = _topicNameFormatter.GetTopicName(topicName);

            var subscriptionPath = string.Format("{0}/{1}", topicName, subscriptionConfiguration.SubscriptionName);

            _logService.Info("Subscribing to topic/subscription '{0}'", subscriptionPath);

            var topicClient = _clientFactory.CreateFromConnectionString(
                serviceBusConfiguration.ConnectionString, topicName);

            var options = new OnMessageOptions
            {
                MaxConcurrentCalls =
                    subscriptionConfiguration.MaxConcurrentMessagesPerNode ??
                    serviceBusConfiguration.DefaultMaxConcurrentMessagesPerNode,
                AutoComplete = false
            };

            options.ExceptionReceived += LogSubscriptionClientException;

            var subscriptionClient = _clientFactory.CreateFromConnectionString(
                serviceBusConfiguration.ConnectionString,
                topicName,
                subscriptionConfiguration.SubscriptionName,
                ReceiveMode.PeekLock);

            var subscriberInfo = new SubscriberInfo
            {
                SubscriptionPath   = subscriptionPath,
                TopicClient        = topicClient,
                SubscriptionClient = subscriptionClient,
                Subscriber         = subscriber
            };

            subscriptionClient.OnMessage(brokeredMessage => ConsumeMessage(subscriberInfo, brokeredMessage), options);

            _logService.Info("Subscribed to topic/subscription '{0}' with max of {1} concurrent call(s) per node",
                             subscriptionPath, options.MaxConcurrentCalls);

            return(subscriberInfo);
        }