diff --git a/docs/azure/sdk/dependency-injection.md b/docs/azure/sdk/dependency-injection.md index e5fab40e25b58..eee1054a2b973 100644 --- a/docs/azure/sdk/dependency-injection.md +++ b/docs/azure/sdk/dependency-injection.md @@ -26,12 +26,13 @@ To register and configure service clients from an [`Azure.`-prefixed package](pa dotnet add package Azure.Identity ``` -For demonstration purposes, the sample code in this article uses the Key Vault Secrets, Blob Storage, and Service Bus libraries. Install the following packages to follow along: +For demonstration purposes, the sample code in this article uses the Key Vault Secrets, Blob Storage, Service Bus, and Azure OpenAI libraries. Install the following packages to follow along: ```dotnetcli dotnet add package Azure.Security.KeyVault.Secrets dotnet add package Azure.Storage.Blobs dotnet add package Azure.Messaging.ServiceBus +dotnet add package Azure.AI.OpenAI ``` ## Register clients and subclients @@ -42,15 +43,15 @@ In the *Program.cs* file, invoke the , and , respectively. The `Uri`- and `string`-typed arguments are passed. To avoid specifying these URLs explicitly, see the [Store configuration separately from code](#store-configuration-separately-from-code) section. * is used to satisfy the `TokenCredential` argument requirement for each registered client. When one of the clients is created, `DefaultAzureCredential` is used to authenticate. * Service Bus subclients are registered for each queue on the service using the subclient and corresponding options types. The queue names for the subclients are retrieved using a separate method outside of the service registration because the `GetQueuesAsync` method must be run asynchronously. +* An Azure OpenAI client is registered using a custom client factory via the method, which provides control over how a client instance is created. Custom client factories are useful in the following cases: + * You need to use other dependencies during the client construction. + * A registration extension method doesn't exist for the service client you want to register. ## Use the registered clients diff --git a/docs/azure/sdk/snippets/dependency-injection/Directory.Packages.props b/docs/azure/sdk/snippets/dependency-injection/Directory.Packages.props index ac9e77385f02c..541365e33bb1a 100644 --- a/docs/azure/sdk/snippets/dependency-injection/Directory.Packages.props +++ b/docs/azure/sdk/snippets/dependency-injection/Directory.Packages.props @@ -11,5 +11,6 @@ + \ No newline at end of file diff --git a/docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/HostApplicationBuilder.csproj b/docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/HostApplicationBuilder.csproj index 26337206feca1..1a6a6793f6f30 100644 --- a/docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/HostApplicationBuilder.csproj +++ b/docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/HostApplicationBuilder.csproj @@ -14,5 +14,6 @@ + diff --git a/docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/Program.cs b/docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/Program.cs index c4a061428e197..09df8f2a4b300 100644 --- a/docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/Program.cs +++ b/docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/Program.cs @@ -3,21 +3,24 @@ using Azure.Messaging.ServiceBus.Administration; using Microsoft.Extensions.Azure; using Microsoft.Extensions.Hosting; - -List queueNames = await GetQueueNames(); +using Azure.AI.OpenAI; IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices(services => { - services.AddAzureClients(clientBuilder => + services.AddAzureClients(async clientBuilder => { // Register clients for each service clientBuilder.AddSecretClient(new Uri("")); clientBuilder.AddBlobServiceClient(new Uri("")); clientBuilder.AddServiceBusClientWithNamespace(".servicebus.windows.net"); - clientBuilder.UseCredential(new DefaultAzureCredential()); + + // Set a credential for all clients to use by default + DefaultAzureCredential credential = new(); + clientBuilder.UseCredential(credential); // Register subclients for Service Bus + List queueNames = await GetQueueNames(credential); foreach (string queueName in queueNames) { clientBuilder.AddClient((_, _, provider) => @@ -27,16 +30,21 @@ _ => throw new InvalidOperationException("Unable to create ServiceBusClient") }).WithName(queueName); } + + // Register a custom client factory + clientBuilder.AddClient( + (options, _, _) => new AzureOpenAIClient( + new Uri(""), credential, options)); }); }).Build(); await host.RunAsync(); -async Task> GetQueueNames() +async Task> GetQueueNames(DefaultAzureCredential credential) { // Query the available queues for the Service Bus namespace. var adminClient = new ServiceBusAdministrationClient - (".servicebus.windows.net", new DefaultAzureCredential()); + (".servicebus.windows.net", credential); var queueNames = new List(); // Because the result is async, the queue names need to be captured diff --git a/docs/azure/sdk/snippets/dependency-injection/HostBuilder/HostBuilder.csproj b/docs/azure/sdk/snippets/dependency-injection/HostBuilder/HostBuilder.csproj index 686e38fed040e..7a1ca703d9b7f 100644 --- a/docs/azure/sdk/snippets/dependency-injection/HostBuilder/HostBuilder.csproj +++ b/docs/azure/sdk/snippets/dependency-injection/HostBuilder/HostBuilder.csproj @@ -14,5 +14,6 @@ + diff --git a/docs/azure/sdk/snippets/dependency-injection/HostBuilder/Program.cs b/docs/azure/sdk/snippets/dependency-injection/HostBuilder/Program.cs index 6fa1e415cce9f..2c7f1779c115a 100644 --- a/docs/azure/sdk/snippets/dependency-injection/HostBuilder/Program.cs +++ b/docs/azure/sdk/snippets/dependency-injection/HostBuilder/Program.cs @@ -3,37 +3,45 @@ using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; using Microsoft.Extensions.Azure; - -List queueNames = await GetQueueNames(); +using Azure.AI.OpenAI; IHost host = Host.CreateDefaultBuilder(args) .ConfigureServices(services => { - services.AddAzureClients(clientBuilder => + services.AddAzureClients(async clientBuilder => { // Register clients for each service clientBuilder.AddSecretClient(new Uri("")); clientBuilder.AddBlobServiceClient(new Uri("")); clientBuilder.AddServiceBusClientWithNamespace(".servicebus.windows.net"); - clientBuilder.UseCredential(new DefaultAzureCredential()); + + // Set a credential for all clients to use by default + DefaultAzureCredential credential = new(); + clientBuilder.UseCredential(credential); // Register a subclient for each Service Bus Queue + List queueNames = await GetQueueNames(credential); foreach (string queue in queueNames) { clientBuilder.AddClient((_, _, provider) => provider.GetService().CreateSender(queue) ).WithName(queue); } + + // Register a custom client factory + clientBuilder.AddClient( + (options, _, _) => new AzureOpenAIClient( + new Uri(""), credential, options)); }); }).Build(); await host.RunAsync(); -async Task> GetQueueNames() +async Task> GetQueueNames(DefaultAzureCredential credential) { // Query the available queues for the Service Bus namespace. var adminClient = new ServiceBusAdministrationClient - (".servicebus.windows.net", new DefaultAzureCredential()); + (".servicebus.windows.net", credential); var queueNames = new List(); // Because the result is async, the queue names need to be captured diff --git a/docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/Program.cs b/docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/Program.cs index 3dd7f253c4ba2..5e382e2565f8a 100644 --- a/docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/Program.cs +++ b/docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/Program.cs @@ -3,36 +3,44 @@ using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; using Microsoft.Extensions.Azure; +using Azure.AI.OpenAI; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); -List queueNames = await GetQueueNames(); - -builder.Services.AddAzureClients(clientBuilder => +builder.Services.AddAzureClients(async clientBuilder => { // Register clients for each service clientBuilder.AddSecretClient(new Uri("")); clientBuilder.AddBlobServiceClient(new Uri("")); clientBuilder.AddServiceBusClientWithNamespace( ".servicebus.windows.net"); - clientBuilder.UseCredential(new DefaultAzureCredential()); + + // Set a credential for all clients to use by default + DefaultAzureCredential credential = new(); + clientBuilder.UseCredential(credential); // Register a subclient for each Service Bus Queue + List queueNames = await GetQueueNames(credential); foreach (string queue in queueNames) { clientBuilder.AddClient( (_, _, provider) => provider.GetService() - .CreateSender(queue)).WithName(queue); + .CreateSender(queue)).WithName(queue); } + + // Register a custom client factory + clientBuilder.AddClient( + (options, _, _) => new AzureOpenAIClient( + new Uri(""), credential, options)); }); WebApplication app = builder.Build(); -async Task> GetQueueNames() +async Task> GetQueueNames(DefaultAzureCredential credential) { // Query the available queues for the Service Bus namespace. var adminClient = new ServiceBusAdministrationClient - (".servicebus.windows.net", new DefaultAzureCredential()); + (".servicebus.windows.net", credential); var queueNames = new List(); // Because the result is async, the queue names need to be captured diff --git a/docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/WebApplicationBuilder.csproj b/docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/WebApplicationBuilder.csproj index 82858f1c05434..e2a7a1732a2b1 100644 --- a/docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/WebApplicationBuilder.csproj +++ b/docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/WebApplicationBuilder.csproj @@ -14,6 +14,7 @@ +