Could you please provide more details about how to use client libraries in the ASP .NET Core project?
There is a little bit confusion about how to properly register types in the ASP .NET Core IoC container.
There are clinets like ServiceBusClient, EventHubProducerClient or EventHubConsumerClient and they implement IAsyncDisposable. How should they be properly disposed on the application shutdown?
I found the thread on GitHub with information that client libraries can be registered as singletons. Fine but how to dispose them properly then when they are registered manually?
Example:
var serviceBusClient = new ServiceBusClient(serviceBusConfiguration.ListenAndSendConnectionString);
services.TryAddSingleton(serviceBusClient);
How to use these types in the BackgroundService for instance? I would be grateful for more details.
I know that there is Microsoft.Extensions.Azure package but it does not support all cases (like ServiceBus client registration).
/cc: @JoshLove-msft, @ShivangiReja
but it does not support all cases (like ServiceBus client registration).
I think this is being added right now.
Adding clients to DI as singleton would work most of the time because Microsoft.Extensions.DependencyInjection has support for IAsyncDisposable do you client would be disposed on shutdown.
Thank you @pakrym for the update and answer. Yes this is correct what you said but it would be nice to include this information in the documentation.
There is support for disposing IAsyncDisposables but you have to follow the guides related with DI container in ASP .NET Core available here:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1#service-registration-methods
I created project called Azure Developer Starter Pack that includes registration of new Azure SDK objects, here is the sample with Azure Service Bus but whole project contains registration samples for other Azure service clients:
Closing, feel free to reopen or file a new issues if you have more questions.