Skip to content
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

Error with "Detect server-side transient disposables" sample code when using keyed services #32490

Closed
nwoolls opened this issue May 1, 2024 · 7 comments · Fixed by #32510
Closed
Assignees
Labels

Comments

@nwoolls
Copy link

nwoolls commented May 1, 2024

Description

Hi there - I am trying out the suggested code for detecting transient disposables. However, the example code does not work if keyed services are registered w/ DI. Instead, the following exception is thrown:

An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Extensions.DependencyInjection.Abstractions.dll: 'This service descriptor is keyed. Your service provider may not support keyed services.'

You can reproduce this using the following code in Program.cs:

builder.DetectIncorrectUsageOfTransients();
builder.Services.AddTransient<TransientDependency>();
builder.Services.AddTransient<ITransitiveTransientDisposableDependency,
   TransitiveTransientDisposableDependency>();

builder.Services.AddKeyedTransient<SaladChef>("foo");

Is it possible to update the Blazor sample code to handle keyed services while still detecting transient disposables?

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/fundamentals/dependency-injection.md

Document ID

d9aabe95-69e9-0a03-81ff-429914137f9d

Article author

@guardrex

Copy link
Contributor

github-actions bot commented May 1, 2024

🕺💃 Happy Cinco de Mayo! 🥳🎈

A green dinosaur 🦖 will be along shortly to assist. Stand-by ........

@guardrex
Copy link
Collaborator

guardrex commented May 1, 2024

Thanks for the issue, @nwoolls. I'll take a look as soon as I can, but it might be a week before I can reach this.

@nwoolls
Copy link
Author

nwoolls commented May 2, 2024

Sounds good @guardrex - FWIW something like this seems to fix the error and make the detection work for keyed services that are transient disposables:

foreach (var descriptor in containerBuilder)
{
    switch (descriptor.Lifetime)
    {
        case ServiceLifetime.Transient 
            when (descriptor is { IsKeyedService: true, KeyedImplementationType: not null }
                  && typeof(IDisposable).IsAssignableFrom(descriptor.KeyedImplementationType))
                 || (descriptor is { IsKeyedService: false, ImplementationType: not null }
                     && typeof(IDisposable).IsAssignableFrom(descriptor.ImplementationType)):
            collection.Add(CreatePatchedDescriptor(descriptor));
            break;
        case ServiceLifetime.Transient
            when descriptor is { IsKeyedService: true, KeyedImplementationFactory: not null }
                or { IsKeyedService: false, ImplementationFactory: not null }:
            collection.Add(CreatePatchedFactoryDescriptor(descriptor));
            break;
        default:
            collection.Add(descriptor);
            break;
    }
}

@guardrex
Copy link
Collaborator

guardrex commented May 2, 2024

Thanks @nwoolls ... That will speed it up quite a bit 🏃‍♂️!

@nwoolls
Copy link
Author

nwoolls commented May 2, 2024

No problem - let me know if you'd like a PR for the samples. Not sure if that repo generally accepts community contributions since it's MS docs.

@guardrex
Copy link
Collaborator

guardrex commented May 2, 2024

Fortunately, I don't think that the text will require an update for this. Also, it only pertains to the one pair of sample apps at 8.0. If you want to open an issue on the samples repo, and then provide a PR there, that would be welcome. I'm usually ⛰️⛏️😅 and appreciate all of the community contributions that come in.

The code is in the first two sample apps ...

https://github.com/dotnet/blazor-samples/tree/main/8.0

@guardrex
Copy link
Collaborator

guardrex commented May 3, 2024

Addressed in the sample apps via dotnet/blazor-samples#283, and I'm going to update the article date now to confirm that the new code is pulled into the article. This will close automatically in a moment.

Thanks again, @nwoolls! 🎷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants