# 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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
