public static AzureServiceBusReceiver Create(AzureServiceBusConfiguration config, Func <IMessageReceiver, Message, CancellationToken, Task> receiveFunc, Func <ExceptionReceivedEventArgs, Task> errorFunc)
        {
            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(config.KeyName, config.SharedAccessSignature, TimeSpan.FromDays(1));

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

            MessageReceiver receiver = null;

            if (config.AllowReceiverToOwnConnection)
            {
                receiver = new MessageReceiver(config.Endpoint, config.QueueName, tokenProvider, receiveMode: ReceiveMode.PeekLock, retryPolicy: RetryPolicy.Default)
                {
                    PrefetchCount = config.MaxConcurrentCalls
                };
            }
            else
            {
                receiver = new MessageReceiver(queueConnection, config.QueueName, ReceiveMode.PeekLock, RetryPolicy.Default)
                {
                    PrefetchCount = config.MaxConcurrentCalls
                };
            }

            receiver.RegisterMessageHandler((message, token) => receiveFunc(receiver, message, token), new MessageHandlerOptions(errorFunc)
            {
                AutoComplete       = false,
                MaxConcurrentCalls = config.MaxConcurrentCalls
            });

            return(new AzureServiceBusReceiver(receiver));
        }
Beispiel #2
0
        public static async Task <AzureServiceBusWrapper> InitializeAsync(AzureServiceBusConfiguration configuration, CancellationToken cancellationToken = default)
        {
            var wrapper = new AzureServiceBusWrapper();
            await wrapper.InitializeInternalAsync(configuration, cancellationToken);

            return(wrapper);
        }
Beispiel #3
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();
        }
Beispiel #4
0
        public static async Task Main()
        {
            var config = new AzureServiceBusConfiguration
            {
                QueueName                    = "repro.project.queue",
                TopicName                    = "repro.project.topic",
                MaxConcurrentCalls           = 10,
                Endpoint                     = "sb://endpoint.servicebus.windows.net",
                KeyName                      = "RootManageSharedAccessKey",
                SharedAccessSignature        = "setme",
                PublishInterval              = 5000,
                AllowReceiverToOwnConnection = true
            };

            var wrapper = await AzureServiceBusWrapper.InitializeAsync(config);

            bool shutdown = false;

            while (!shutdown)
            {
                Console.WriteLine("Press d to print diagnostics or press any other key to shut down and exit...");
                var key = Console.ReadKey();
                Console.WriteLine();
                if (key.KeyChar == 'd')
                {
                    await wrapper.PrintRecycledReceiversDiagnosticsAsync();
                }
                else
                {
                    shutdown = true;
                }
            }

            Console.WriteLine("Shutting down...");
            await wrapper.ShutdownAsync();
        }