Skip to content

Commit 3f6b5d1

Browse files
committed
New AddClient example
1 parent 7426cfe commit 3f6b5d1

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

docs/azure/sdk/dependency-injection.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ In the *Program.cs* file, invoke the <xref:Microsoft.Extensions.Azure.AzureClien
4242

4343
### [WebApplicationBuilder](#tab/web-app-builder)
4444

45-
:::code language="csharp" source="snippets/dependency-injection/WebApplicationBuilder/Program.cs" id="snippet_WebApplicationBuilder" highlight="10-26":::
45+
:::code language="csharp" source="snippets/dependency-injection/WebApplicationBuilder/Program.cs" id="snippet_WebApplicationBuilder" highlight="10-31":::
4646

4747
### [HostApplicationBuilder](#tab/host-app-builder)
4848

49-
:::code language="csharp" source="snippets/dependency-injection/HostApplicationBuilder/Program.cs" highlight="12-30":::
49+
:::code language="csharp" source="snippets/dependency-injection/HostApplicationBuilder/Program.cs" highlight="12-35":::
5050

5151
### [HostBuilder](#tab/host-builder)
5252

53-
:::code language="csharp" source="snippets/dependency-injection/HostBuilder/Program.cs" id="snippet_HostBuilder" highlight="11-26":::
53+
:::code language="csharp" source="snippets/dependency-injection/HostBuilder/Program.cs" id="snippet_HostBuilder" highlight="11-31":::
5454

5555
---
5656

@@ -59,6 +59,15 @@ In the preceding code:
5959
* Key Vault Secrets, Blob Storage, and Service Bus clients are registered using the <xref:Microsoft.Extensions.Azure.SecretClientBuilderExtensions.AddSecretClient%2A>, <xref:Microsoft.Extensions.Azure.BlobClientBuilderExtensions.AddBlobServiceClient%2A> and <xref:Microsoft.Extensions.Azure.ServiceBusClientBuilderExtensions.AddServiceBusClientWithNamespace%2A>, 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.
6060
* <xref:Azure.Identity.DefaultAzureCredential> is used to satisfy the `TokenCredential` argument requirement for each registered client. When one of the clients is created, `DefaultAzureCredential` is used to authenticate.
6161
* 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.
62+
* An Azure OpenAI client is registered using a custom client factory through the `AddClient<TClient, TOptions>` method, which provides control over how a client instance is created. Custom client factories are useful when you need to use other dependencies during the client construction, or if a service registration extension method does not exist for the service you want to register.
63+
64+
### Register a custom client factory
65+
66+
If you want to take control over how the client instance is created or need to use other dependencies during the client construction use the `AddClient<TClient, TOptions>` method.
67+
68+
Here's an example of how to use `IOptions<T>` instance to construct the client:
69+
70+
:::code language="csharp" source="snippets/dependency-injection/WebApplicationBuilder/Program.cs" id="snippet_WebApplicationBuilder" highlight="10-26":::
6271

6372
## Use the registered clients
6473

@@ -125,6 +134,7 @@ In the [Register clients and subclients](#register-clients-and-subclients) secti
125134
}
126135
```
127136

137+
128138
You can add any properties from the <xref:Azure.Core.ClientOptions> class into the JSON file. The settings in the JSON configuration file can be retrieved using <xref:Microsoft.Extensions.Configuration.IConfiguration>.
129139

130140
### [WebApplicationBuilder](#tab/web-app-builder)

docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
_ => throw new InvalidOperationException("Unable to create ServiceBusClient")
2828
}).WithName(queueName);
2929
}
30+
31+
// Register a custom client factory
32+
clientBuilder.AddClient<AzureOpenAIClient, AzureOpenAIClientOptions>(
33+
(options, _, _) => new AzureOpenAIClient(
34+
new Uri("<url_here>"), new DefaultAzureCredential(), options));
3035
});
3136
}).Build();
3237

docs/azure/sdk/snippets/dependency-injection/HostBuilder/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
provider.GetService<ServiceBusClient>().CreateSender(queue)
2525
).WithName(queue);
2626
}
27+
28+
// Register a custom client factory
29+
clientBuilder.AddClient<AzureOpenAIClient, AzureOpenAIClientOptions>(
30+
(options, _, _) => new AzureOpenAIClient(
31+
new Uri("<url_here>"), new DefaultAzureCredential(), options));
2732
});
2833
}).Build();
2934

docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
(_, _, provider) => provider.GetService<ServiceBusClient>()
2525
.CreateSender(queue)).WithName(queue);
2626
}
27+
28+
// Register a custom client factory
29+
clientBuilder.AddClient<AzureOpenAIClient, AzureOpenAIClientOptions>(
30+
(options, _, _) => new AzureOpenAIClient(
31+
new Uri("<url_here>"), new DefaultAzureCredential(), options));
2732
});
2833

2934
WebApplication app = builder.Build();

0 commit comments

Comments
 (0)