The sample will remain published as a reference, but both the Azure SDKs and ASP.NET have seen new revisions since this code was published. As this code sample has aged, I have chosen to archive this repo.
This project provides a Web API (ASP.NET Core 2) that submits messages to Service Bus, and then has background worker service created as a IHostedService to read from the queue and handle the long-running tasks.
For more details, see Background tasks with hosted services in ASP.NET Core.
This is a code sample and not indended for production use as-is. Some components are missing, for example:
- Connection clean-up on shutdown
- Interface to support Event Hub or other enterprise messaging bus implementations
- Exception and error handling
- Aritrary message types
Due to the way the .NET Azure SDK is coded, the message auto-renew timer continues in the background even if messages are completed. This can result in harmless MessageLockLostException
exceptions, especially for some long-running message handlers:
Microsoft.Azure.ServiceBus.MessageLockLostException: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue.
The SDK authors have determined this behavior is as designed. See Azure/azure-sdk-for-net#6723 for details.
This code sample is released under the MIT license.