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
Comments
🕺💃 Happy Cinco de Mayo! 🥳🎈A green dinosaur 🦖 will be along shortly to assist. Stand-by ........ |
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. |
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;
}
} |
Thanks @nwoolls ... That will speed it up quite a bit 🏃♂️! |
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. |
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 ... |
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! 🎷 |
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:
You can reproduce this using the following code in
Program.cs
: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
The text was updated successfully, but these errors were encountered: