From f5813620016a2afc51045c8575633af36e2b3276 Mon Sep 17 00:00:00 2001 From: lilyjma Date: Fri, 4 Jun 2021 12:24:25 -0700 Subject: [PATCH] Update README.md with DI content (#21410) * Update README.md adding dependency injection section * Update README.md Proposing changes based on the chat with @KrzysztofCwalina and @davidfowl. The changes are around simplifying the samples and using the configuration instead of the hard coded connection string. * Update README.md Change the method named used and show the configuration snippet as well. * Update sdk/servicebus/Azure.Messaging.ServiceBus/README.md Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> * Update sdk/servicebus/Azure.Messaging.ServiceBus/README.md Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> * minor edits Co-authored-by: Alex Ghiondea Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com> --- .../Azure.Messaging.ServiceBus/README.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/README.md b/sdk/servicebus/Azure.Messaging.ServiceBus/README.md index 32aa68f0ca882..31767ba20a176 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/README.md +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/README.md @@ -56,6 +56,34 @@ ServiceBusClient client = new ServiceBusClient(connectionString); To see how to authenticate using Azure.Identity, view this [example](#authenticating-with-azureidentity). +### ASP.NET Core + +To inject `ServiceBusClient` as a dependency in an ASP.NET Core app, register the client in the `Startup.ConfigureServices` method: + +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddAzureClients(builder => + { + builder.AddServiceBusClient(Configuration.GetConnectionString("ServiceBus")); + }); + + services.AddControllers(); +} +``` + +To use the preceding code, add this to your configuration: + +```json +{ + "ConnectionStrings": { + "ServiceBus": "" + } +} +``` + +For more details, see [Dependency injection with the Azure SDK for .NET](https://docs.microsoft.com/dotnet/azure/sdk/dependency-injection). + ## Key concepts Once you've initialized a `ServiceBusClient`, you can interact with the primary resource types within a Service Bus Namespace, of which multiple can exist and on which actual message transmission takes place, the namespace often serving as an application container: