Exemple #1
0
        private async Task InitializeInternalAsync(AzureServiceBusConfiguration configuration, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            _config = configuration;

            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(_config.KeyName, _config.SharedAccessSignature, TimeSpan.FromDays(1));

            _managementClient = new ManagementClient(_config.Endpoint, tokenProvider);

            if (!await _managementClient.QueueExistsAsync(_config.QueueName, cancellationToken))
            {
                await _managementClient.CreateQueueAsync(_config.QueueName, cancellationToken);
            }

            if (!await _managementClient.TopicExistsAsync(_config.TopicName, cancellationToken))
            {
                await _managementClient.CreateTopicAsync(_config.TopicName, cancellationToken);
            }

            if (!await _managementClient.SubscriptionExistsAsync(_config.TopicName, _config.TopicName, cancellationToken))
            {
                await _managementClient.CreateSubscriptionAsync(new SubscriptionDescription(_config.TopicName, _config.TopicName)
                {
                    ForwardTo = _config.QueueName
                }, cancellationToken);
            }

            var queueConnection = new ServiceBusConnection(_config.Endpoint, TransportType.Amqp, RetryPolicy.Default)
            {
                TokenProvider = tokenProvider
            };

            _messageReceiver = AzureServiceBusReceiver.Create(_config, ReceiveMessageAsync, HandleErrorAsync);

            var topicConnection = new ServiceBusConnection(_config.Endpoint, TransportType.Amqp, RetryPolicy.Default)
            {
                TokenProvider = tokenProvider
            };

            _messageSender             = new TopicClient(topicConnection, _config.TopicName, RetryPolicy.Default);
            _messageSendTimer          = new System.Timers.Timer(_config.PublishInterval);
            _messageSendTimer.Elapsed += SendMessageAsync;

            _messageSendTimer.Start();
        }
Exemple #2
0
        private async Task RecyleReceiverAsync()
        {
            try
            {
                await _recycleLock.WaitAsync();

                Console.WriteLine("Recycling the Receiver");

                await _messageReceiver.CloseAsync();

                _closedReceivers.Add(_messageReceiver);
                _messageReceiver = AzureServiceBusReceiver.Create(_config, ReceiveMessageAsync, HandleErrorAsync);
            }
            finally
            {
                _recycleLock.Release();
            }
        }