Beispiel #1
0
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            string endpoint      = _settings.ServiceBusNamespace + ".servicebus.windows.net";
            string queueName     = _settings.ServiceBusQueueName;
            var    tokenProvider = new ManagedIdentityServiceBusTokenProvider(_settings.ManagedIdentityTenantId);
            var    queueClient   = new QueueClient(endpoint, queueName, tokenProvider, TransportType.Amqp, ReceiveMode.PeekLock);

            try
            {
                var messageHandlerOptions = new MessageHandlerOptions(HandleException)
                {
                    AutoComplete = true
                };
                queueClient.RegisterMessageHandler(HandleMessage, messageHandlerOptions);
                await Task.Delay(-1, cancellationToken);
            }
            catch (UnauthorizedException e)
            {
                // Log and exit
            }
            finally
            {
                await queueClient.CloseAsync();
            }
        }
        public async Task SendServiceBusQueueMessage(string messageString)
        {
            string endpoint  = _settings.ServiceBusNamespace + ".servicebus.windows.net";
            string queueName = _settings.ServiceBusQueueName;

            var tokenProvider = new ManagedIdentityServiceBusTokenProvider(_settings.ManagedIdentityTenantId);
            var queueClient   = new QueueClient(endpoint, queueName, tokenProvider);

            var message = new Microsoft.Azure.ServiceBus.Message(
                Encoding.UTF8.GetBytes($"{messageString} ({DateTime.UtcNow:HH:mm:ss})"));
            await queueClient.SendAsync(message);
        }