microsoft / microsoft/aspire

Azure Components don't support keyed and unkeyed services

Open
#3,890 2 comments 2 reactions 0 assignees View on GitHub
area-integrations azure
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

When adding both a keyed and an unkeyed client for the same Azure client type, it is throwing an exception:

### Repro:

Running the following test:

```C#
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Configuration.AddInMemoryCollection([
new KeyValuePair("ConnectionStrings:blob1", "https://aspirestoragetests.blob.core.windows.net/"),
new KeyValuePair("ConnectionStrings:blob2", "https://aspirestoragetests2.blob.core.windows.net/"),
]);

builder.AddAzureBlobClient("blob1");
builder.AddKeyedAzureBlobClient("blob2");

using var host = builder.Build();

var client1 = host.Services.GetRequiredService();
var client2 = host.Services.GetRequiredKeyedService("blob2");

Assert.NotSame(client1, client2);

Assert.Equal("aspirestoragetests", client1.AccountName);
Assert.Equal("aspirestoragetests2", client2.AccountName);
```

Results in:

```
Message: 
System.InvalidOperationException : No service for type 'Azure.Storage.Blobs.BlobServiceClient' has been registered.

Stack Trace: 
ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
AspireBlobStorageExtensionsTests.CanAddMultipleKeyedServices() line 144
```

It looks like the issue is here:

https://github.com/dotnet/aspire/blob/6f5cfb36883e767c55e9d2d33ce67cf3b4c99b2b/src/Components/Common/AzureComponent.cs#L67-L77

There is an explict test for this scenario that fails when I try removing the above line:

https://github.com/dotnet/aspire/blob/6f5cfb36883e767c55e9d2d33ce67cf3b4c99b2b/tests/Aspire.Components.Common.Tests/ConformanceTests.cs#L229-L247

@tg-msft - is this something that can be addressed in Microsoft.Extensions.Azure? Maybe it could officially support keyed DI services? Then we wouldn't have to try to work around it using `.WithName` on the Aspire side.

cc @DamianEdwards @davidfowl @sebastienros @radical @adamsitnik

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.