-
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
ServiceDescriptor.ImplementationType throws exception for Keyed descriptor: System.InvalidOperationException: This service descriptor is keyed. Your service provider may not support keyed services. #95789
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsDescriptionIt looks like an intentional behavior is breaking a lot of existing code. There are a lot of places that inspect/iterate over ServiceCollection and look for a specific ImplementationType as a way of custom TryAdd. This works fine with This is happening on all frameworks supported by Now, I understand that this is not a good practice (iterating and inspecting ImplementationType). Sadly, it seems to be a common practice, even including multiple Microsoft products:
Problematic source code: runtime/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceDescriptor.cs Lines 191 to 201 in 611934f
Reproduction Steps
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddKeyedSingleton<FooService>("key");
services.FirstOrDefault(x => x.ImplementationType == typeof(BarService));
class FooService {}
class BarService {}
Expected behaviorExisting libraries keep working - exception is not thrown. Actual behavior
Regression?Definitely a regression. This works with all versions of DependencyInjection up until v8. Known WorkaroundsAttempt to register keyed service after registrations that inspect ServiceCollection. ConfigurationNo response Other informationNo response
|
I also encountered it, I think this is a bug, KeyedImplementationType needs to check whether KeyedService is true, but ImplementationType should not be checked |
@benjaminpetit PTAL |
Is there a way to make this problem compatible? It makes upgrading net8.0 even more difficult? Now I don’t dare to use the AddKeyedSingleton method in the project anymore |
I am having the same problem here. private IEnumerable<Type> GetSingletons(IServiceCollection services)
{
return services
.Where(descriptor => descriptor.Lifetime == ServiceLifetime.Singleton)
// exception here: "This service descriptor is keyed. Your service provider may not support keyed services."
.Where(descriptor => descriptor.ImplementationType != typeof(WarmupService))
.Where(descriptor => descriptor.ServiceType.ContainsGenericParameters == false)
.Select(descriptor => descriptor.ServiceType)
.Distinct();
} |
We throw when you try to access non-keyed property on a keyed descriptor, and when you try to access keyed property on a non keyed descriptor. It's not a bug, it's to make sure that if the container doesn't support keyed DI it will not build if there is keyed descriptors. You need to check the value of the property |
@benjaminpetit I understand what was the intent - but the result is that it throws not when the container doesn't support Keyed DI, but if any components that attempt registration aren't prepared to test keyed-ness. Which makes it a breaking change for a lot of cases. What you're saying is completely unhelpful for people who want to use an older component that performs such a registration under the hood. Can you say why returning a |
It's only a breaking change if you are using keyed registration. Would it be better to return I don't think there is a right or a wrong solution here. I think throwing an exception is safer, but I understand that it's painful if you cannot update an older component. Maybe in future version we could make it configurable @steveharter ? |
Right now it's a breaking change if I'm using keyed registration, indeed. But soon, as Orlean's linked issue suggests, other components (external to my own codebase) will use Keyed, and it will collide with me using any other external component which doesn't know a thing about Keyed registrations but still performs the check as I've mentioned in OP. Which, assuming I can't just stop using existing dependencies, means I'm blocked from upgrading at all. This means that soon, I'll be unable to use new components that opted into Keyed registrations, if older components don't update to check for Keyedness. And all this is well before actual container is involved. We're having issues with registrations which are keyedness-oblivious, while the container does/would support them. I can't really imagine how could a container that doesn't support keyed services not throw/fail if all of PS Of course, the error message would be more confusing, as it'd come from the container instead of a registration, but if that's the cost of compatibility... I've seen worse. |
See AzureAD/microsoft-identity-web#2604. Many packages are not updated to take keyed services into account, causing exceptions based on the order in which you register servcies. Throwing an exception may be the right solution when all packages are taking keyed services into account. |
Another issue I just ran into. Calling JsonConvert.SerializeObject() on the ServiceDescriptor is bound to throw error every time, as one of the two getters (ImplementationType, KeyedImplementationType) is always gonna throw exception. This is breaking our existing code. |
@benjaminpetit : Many packages that are looking up services need to be updated. Until then keyed services are virtually impossible to use with the current behaviour of throwing exceptions. |
An exception when calling a property getter is unusual and it kind of threw me until I saw what was going on in the source code. I think a more helpful exception message would work wonders. The current message is just too vague. However, you explain it well above. How about something like "You are trying to read a property that is not supported when using a keyed descriptor. Consider accessing the same property name prefixed by "Keyed", instead. You may receive this exception if your service provider does not support keyed services. " |
Orleans 8 has just been released and it's impossible to use it in the same project as Application Insights due to this issue. @ReubenBond |
@jkonecki it sounds like this would be fixed by an update to the Application Insights SDK, can you confirm? If so, let's find out who we need to talk to and see if we can get the fix in, @benjaminpetit |
@ReubenBond @benjaminpetit I'm using the latest ApplicationInsights 2.22.0. I cannot see any newer pre-release packages. Actually AppInsights was a red herring. The issue is caused by Microsoft.Identity.Web
The workaround for me was to Identity Web registration before Orleans client registration. |
@ReubenBond @benjaminpetit @jkonecki
Yeah, that is a workaround for now, but a totally counterintuitive one. And often not possible/difficult to implement in bigger applications. Registration of services in a container should not have a required order. For now, keyed services are just impossible for us to use now, given that there are too many packages around that are not compatible with keyed services. |
I am also experiencing this issue when using Orleans 7, calling The work-around does not help in my case. (edit) Never mind. This turned out to be an Autofac issue in my case. |
@rogereeno would you mind sharing your solution? it seems like i've got exactly the same issue with Autofac. |
I updated to the latest version of Autofac packages. |
I just spent a day debugging some changes unrelated to Application Insights that was hit by this bug. Luckily the changes never made it to production, since the application crashed on startup due to this setup in our Serilog config: WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Traces) The After hours of debugging to figure out what I changed to cause this issue, I isolated it to adding resilience to an HttpClient with Either The workaround of having to register Application Insights (or anything else not updated to handle keyed services) first, before anything using keyed service, is not acceptable in the long run. This will not scale as keyed services usage increases in the future. |
Ran into this issue today entirely within third party packages.
This was manifesting with an Seems like a huge oversight to throw an exception in this case. Especially in a property getter that seems so innocuous to consumers. I'm sure this is causing a lot of frustration in the community. |
I have also ran into this, I had to rollback my Polly updates to 8.1.0 |
I also subscribe to the fact that this exception driven approach should not have be included in the .NET version. This breaks so many third party libs. And to make the thing worse, some packages will throw the exception, but for example Microsoft.ApplicationInsights.AspNetCore (microsoft/ApplicationInsights-dotnet#2828) will just silently fail, without any warning and no telemetry will be logged anymore. |
Same issue here. We use the service collection to find services which implement one of our interfaces. Our services are not keyed, so now we need to exclude keyed services. This change breaks third party integrations and should be reverted IMO. |
@benjaminpetit what is the plan for this? These issues are blocking us upgrading from .NET 6 to .NET 8 because of nuget packages that we use attempting to iterate through the services and check the ImplementationType which is now causing them to crash. |
This should be fixed for v9. |
Do you mean .NET 9? Will not be included as a fix for .NET 8? |
Frankly - if this were to only be fixed in .NET 9 and .NET 8 would be ignored, that would be completely unacceptable, considering that .NET 8 is an LTS version and will outlive .NET 9 for many production-use environments. |
I didn't say it wouldn't be fixed in v8; we need to get it into v9 first. |
My team also just stumbled into this problem. Can someone explain why it was necessary in the first place to add |
Thank you so much for this decision and change. Will this thread receive an update when it gets back ported to .NET 8? Or is tracking release notes the only option? 🤗 |
Yes this thread will be referenced during a backport. |
One forcing factor is the factory property has different signatures: Line 152 in bd29323
vs Line 157 in bd29323
|
just to add another scenario, starting a brand new ASP.NET Core WebAPI on .NET 8 with both Aspire and Microsoft.Identity.Web bits for EntraID scaffolded via the GUI while creating the solution in VS2022 runs into this exception on the very first run with no code changes. |
I too ran into this issue when creating my first Aspire application and incorporating my existing code. Looks like a fix is on the way. Thank you for all your efforts out there. 🙏 |
Description
It looks like an intentional behavior is breaking a lot of existing code. There are a lot of places that inspect/iterate over ServiceCollection and look for a specific ImplementationType as a way of custom TryAdd. This works fine with
Microsoft.Extensions.DependencyInjection.Abstractions
v8.0.0, until a Keyed descriptor is added before the offending inspection.This is happening on all frameworks supported by
Microsoft.Extensions.DependencyInjection.Abstractions
(v8) - it's not framework-dependent.Now, I understand that this is not a good practice (iterating and inspecting ImplementationType). Sadly, it seems to be a common practice, even including multiple Microsoft products:
Problematic source code:
runtime/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceDescriptor.cs
Lines 158 to 168 in 611934f
Reproduction Steps
dotnet new console
dotnet add package Microsoft.Extensions.DependencyInjection
Program.cs
dotnet run
throws:Expected behavior
Existing libraries keep working - exception is not thrown. Maybe just return null.
Actual behavior
InvalidOperationException
is thrown when iterating over registered descriptors'ImplementationType
.Regression?
Definitely a regression. This works with all versions of DependencyInjection up until v8.
Known Workarounds
Attempt to register keyed service after registrations that inspect ServiceCollection.
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: