-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE REQ] Support keyed DI with AddAzureClients #40408
Comments
Thank you for your feedback, @Arithmomaniac. We've added this to our backlog for consideration. |
@AlexanderSher: Would you please take a look at whether it is feasible to support the new keyed DI registration without breaking changes and advise? |
Hi @Arithmomaniac. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue. |
@Arithmomaniac -- thanks for your feature request! As we look further into different approaches to enabling your scenario, it would be helpful for us to have a clear picture in our heads of what you'd like to do with keyed DI registration. Would it be possible to give us a sense of the type of app that you're building around the DI container and what scenarios/features you are looking to implement that you are unable to accomplish today without keyed registration support? Many thanks in advance for your help on this! |
I've moved on to other projects, but IIRC the use case was pretty similar to the one provided in the DI documentation. Suppose the following DI configuration: builder.Services.AddAzureClients(clientBuilder =>
{
clientBuilder.AddBlobServiceClient(
builder.Configuration.GetSection("PublicStorage"));
.WithName("PublicStorage");
clientBuilder.AddBlobServiceClient(
builder.Configuration.GetSection("PrivateStorage"))
.WithName("PrivateStorage");
}); Today you have to do the following: public class HomeController : Controller
{
private readonly ICache _bigCache;
private readonly BlobServiceClient _publicStorage;
private readonly BlobServiceClient _privateStorage;
public HomeController(
[FromKeyedServices("big")] ICache bigCache,
IAzureClientFactory<BlobServiceClient> clientFactory)
{
_bigCache = bigCache;
_publicStorage = clientFactory.CreateClient("PublicStorage");
_privateStorage = clientFactory.CreateClient("PrivateStorage");
}
} I'd like to be able to do the following: public class HomeController(
[FromKeyedServices("big")] ICache bigCache,
[FromKeyedServices("PublicStorage")] BlobServiceClient publicStorage,
[FromKeyedServices("PrivateStorage")] BlobServiceClient privateStorage
) : Controller
{
} Advantages:
|
Thank you so much for the explanation, @Arithmomaniac! Just to confirm that I am understanding correctly -- Microsoft.Extensions.Azure APIs strictly-speaking support the scenario you're interested in (adding multiple instances of a single client type to the DI service collection) via the Is that correct? |
Hi @Arithmomaniac. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue. |
Precisely |
Looking at this as part of #46671 |
+1 for supporting keyed service by AddAzureClients et al |
While waiting for official support, here's how you can achieve the same. Instead of calling public static class AzureClientBuilderExtensions
{
public static IAzureClientBuilder<TClient, TOptions> WithKey<TClient, TOptions>(
this IAzureClientBuilder<TClient, TOptions> builder, string serviceKey)
where TOptions : class where TClient : class
{
builder.WithName(serviceKey);
var services = (IServiceCollection)builder.GetType().GetProperty("ServiceCollection")!.GetValue(builder)!;
services.AddKeyedSingleton<TClient>(serviceKey,
(provider, _) => provider.GetRequiredService<IAzureClientFactory<TClient>>().CreateClient(serviceKey));
return builder;
}
} |
Library name
Microsoft.Extensions.Azure
Please describe the feature.
When registering multiple services by name (using
WithName()
) with.AddAzureClients
, you need to resolve the intended service via DI by injecting anIAzureClientFactory<TClient>
so that you can invoke.CreateClient()
on the named service.It would be great if registering a dependency by name would register the client with the new keyed DI support so that instances of
TClient
can be injected directly. Or if that poses compatibility issues, add a newWithKey()
method to do the same.The text was updated successfully, but these errors were encountered: