コード例 #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _cancellationToken = cancellationToken;
            _client            = await _clientFactory.GetQueueClient <T>().ConfigureAwait(false);

            if (!await _managementClient.QueueExistsAsync(_client.QueueName, _cancellationToken).ConfigureAwait(false))
            {
                try
                {
                    var serviceBusCreationOptions = GetServiceBusCreationOptions();

                    await _managementClient.CreateQueueAsync(new QueueDescription(_client.Path)
                    {
                        EnableBatchedOperations = serviceBusCreationOptions.EnableBatchedOperations,
                        EnablePartitioning      = serviceBusCreationOptions.EnablePartitioning,
                    }, _cancellationToken).ConfigureAwait(false);
                }
                catch (ServiceBusException e)
                {
                    _log.Error(e, "Failed to create queue {QueueName}", _client.QueueName);
                    throw;
                }
            }
            _deadLetterLimit      = Settings.DeadLetterDeliveryLimit;
            _client.PrefetchCount = Settings.PrefetchCount;

            _messageReceiver = new StoppableMessageReceiver(_client.ServiceBusConnection, _client.QueueName, ReceiveMode.PeekLock, null, Settings.PrefetchCount);

            var options = new StoppableMessageReceiver.MessageHandlerOptions(OnExceptionReceivedAsync)
            {
                AutoComplete         = false,
                MaxAutoRenewDuration = Settings.MessageLockTimeout,
                MaxConcurrentCalls   = Settings.MaxConcurrentCalls
            };

            _messageReceiver.RegisterStoppableMessageHandler(options, Handler);

#pragma warning disable 4014
            // ReSharper disable once MethodSupportsCancellation
            Task.Run(() =>
            {
                _cancellationToken.WaitHandle.WaitOne();
                //Cancellation requested
                try
                {
                    _log.Information($"Closing ServiceBus channel receiver for {typeof(T).Name}");
                    _messageReceiver.StopPump();
                }
                catch (Exception)
                {
                    //Swallow
                }
            });
#pragma warning restore 4014
        }
コード例 #2
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _cancellationToken = cancellationToken;
            _client            = await _clientFactory.GetSubscriptionClient <TTopic, IEventSubscription <TTopic> >(_subscription).ConfigureAwait(false);

            if (!await _managementClient.TopicExistsAsync(_client.TopicPath, cancellationToken).ConfigureAwait(false))
            {
                try
                {
                    var serviceBusCreationOptions = GetServiceBusCreationOptions();

                    await _managementClient.CreateTopicAsync(new TopicDescription(_client.TopicPath)
                    {
                        EnableBatchedOperations = serviceBusCreationOptions.EnableBatchedOperations,
                        EnablePartitioning      = serviceBusCreationOptions.EnablePartitioning,
                        SupportOrdering         = serviceBusCreationOptions.SupportOrdering
                    }, cancellationToken).ConfigureAwait(false);
                }
                catch (ServiceBusException e)
                {
                    _log.Error(e, "Failed to create topic {TopicName}", _client.TopicPath);
                    throw;
                }
            }
            if (!await _managementClient.SubscriptionExistsAsync(_client.TopicPath, _client.SubscriptionName, cancellationToken).ConfigureAwait(false))
            {
                try
                {
                    var serviceBusCreationOptions = GetServiceBusCreationOptions();

                    await _managementClient.CreateSubscriptionAsync(new SubscriptionDescription(_client.TopicPath, _client.SubscriptionName)
                    {
                        EnableBatchedOperations = serviceBusCreationOptions.EnableBatchedOperations,
                    }, cancellationToken).ConfigureAwait(false);
                }
                catch (ServiceBusException e)
                {
                    _log.Error(e, "Failed to create subscription {TopicName} {SubscriptionName}", _client.TopicPath, _client.SubscriptionName);
                    throw;
                }
            }

            _deadLetterLimit      = Settings.DeadLetterDeliveryLimit;
            _client.PrefetchCount = Settings.PrefetchCount;

            _messageReceiver = new StoppableMessageReceiver(_client.ServiceBusConnection, EntityNameHelper.FormatSubscriptionPath(_client.TopicPath, _client.SubscriptionName), ReceiveMode.PeekLock, RetryPolicy.Default, Settings.PrefetchCount);

            var options = new StoppableMessageReceiver.MessageHandlerOptions(OnExceptionReceivedAsync)
            {
                AutoComplete         = false,
                MaxAutoRenewDuration = Settings.MessageLockTimeout,
                MaxConcurrentCalls   = Settings.MaxConcurrentCalls
            };

            _messageReceiver.RegisterStoppableMessageHandler(options, OnMessageAsync);

#pragma warning disable 4014
            // ReSharper disable once MethodSupportsCancellation
            Task.Run(() =>
            {
                _cancellationToken.WaitHandle.WaitOne();
                //Cancellation requested
                try
                {
                    _log.Information($"Closing ServiceBus channel receiver for {typeof(TTopic).Name}");
                    _messageReceiver.StopPump();
                }
                catch (Exception)
                {
                    //Swallow
                }
            });
#pragma warning restore 4014
        }