Page cover
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Azure Service Bus

Azure Service Bus Demo

This demo will allow you to connect to service bus and retrieve messages. It's configured only for 1 concurrent message at a time, but is easily configurable.

Setup and configuration for your service bus can be done on the Azure Portal.

Azure Service Bus Setup

  1. Install the package: Azure.Messaging.ServiceBus

  2. Copy the code over and configure the settings

  3. Run and modify!

PerigeeApplication.ApplicationNoInit("SPA", (c) =>
{
    //Service processor
    c.AddAsync("Service Processor", async (ct, l) => {

        await using var clientSB = new ServiceBusClient(c.GetValue<string>("SPA:queueConnection"));
        await using ServiceBusProcessor processor = clientSB.CreateProcessor(c.GetValue<string>("SPA:queueName"), 
            new ServiceBusProcessorOptions { AutoCompleteMessages = false, MaxConcurrentCalls = 1 }
            );

        //When a message is received:
        processor.ProcessMessageAsync += async (args) =>
        {
            //TODO: Write process code here!!
            
            //Then complete
            await args.CompleteMessageAsync(args.Message);
            
            //Or abandon
            //await args.AbandonMessageAsync(args.Message); 
        };
        
        //When an uncaught error is thrown, (write good code and don't let this happen!)
        processor.ProcessErrorAsync += (args) =>
        {
            var l = c.GetLogger<ServiceBusProcessor>();
            l.LogError("Error: {Source}", args.ErrorSource);
            l.LogError("Error: {FQN}", args.FullyQualifiedNamespace);
            l.LogError("Error: {Entity}", args.EntityPath);
            l.LogError("Error: {Exception}", args.Exception.ToString());
            return Task.CompletedTask;
        };

        //Start the process and delay until cancelled
        await processor.StartProcessingAsync(ct);
        while (PerigeeApplication.delayOrCancel(5000, ct)) { }

        //Cancel request: DO NOT send token here, it will cancel the cancel operation.
        await processor.StopProcessingAsync();
        l.LogInformation("Processor stop completed");
        
    }).LinkToConfig("SPA:enabled");
});

And the appsettings.json:

Last updated