> For the complete documentation index, see [llms.txt](https://docs.perigee.software/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.perigee.software/examples-and-demos/azure-service-bus.md).

# 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](https://portal.azure.com).

## Azure Service Bus Setup

1. Install the package: `Azure.Messaging.ServiceBus`
2. Then [install perigee](/getting-started/installation-and-project-setup.md)
3. Copy the code over and configure the settings
4. Run and modify!

```csharp
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 <mark style="color:red;">**appsettings.json**</mark>:

```json
{
  "ConnectionStrings": {    
  },
  "SPA": {
    "queueConnection": "Endpoint=sb://abcdefg.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ReplaceMe",
    "queueName": "myqueuedemo1",
    "enabled": true,
  },
  "Perigee": { "HideConsole": false },
  "Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}]({ThreadName}) {Message:lj}{NewLine}{Exception}"
        }
      }
    ]
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.perigee.software/examples-and-demos/azure-service-bus.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
