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.
Copy 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");
});
Copy {
"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}"
}
}
]
}
}