Skip to content

AnyKey support and service key injection#1475

Merged
tillig merged 35 commits intodevelopfrom
feature/any-key
Feb 12, 2026
Merged

AnyKey support and service key injection#1475
tillig merged 35 commits intodevelopfrom
feature/any-key

Conversation

@tillig
Copy link
Member

@tillig tillig commented Feb 11, 2026

Resolves #1471.

Adds two primary features that allow for improved support for .NET 10:

  1. Direct support for AnyKey: Microsoft.Extensions.DependencyInjection added the concept of AnyKey back in .NET 8, but the behavior of that became far more complex in .NET 10. For example, AnyKey items no longer get returned when resolving collections of AnyKey - they only get returned in individual resolution calls. This made AnyKey much more complex than simply "matching any key," necessitating a more integrated solution with core Autofac.
  2. Ability to inject the service key into constructors or properties: M.E.DI has a [ServiceKey] attribute it uses to inject the service key into an object. With native support for AnyKey this seemed like a valuable feature to also bring into Autofac.

This should all be backwards-compatible with existing Autofac keyed services - it's just new API to support these new features. It should also make the Autofac.Extensions.DependencyInjection layer a bit easier to implement since we can map the Microsoft AnyKey to the Autofac AnyKey and it should "just work."

While working in here I did a couple of one-click optimizations to remove unused usings and similar minor housekeeping things. That increased the size of the PR but seemed not-unreasonable.

@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

❌ Patch coverage is 94.66019% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.57%. Comparing base (17ccc0b) to head (1161e1f).
⚠️ Report is 36 commits behind head on develop.

Files with missing lines Patch % Lines
src/Autofac/ResolutionExtensions.cs 75.00% 2 Missing and 2 partials ⚠️
src/Autofac/Core/KeyedServiceParameterInjector.cs 95.00% 0 Missing and 2 partials ⚠️
src/Autofac/ParameterExtensions.cs 71.42% 1 Missing and 1 partial ⚠️
...Resolving/Pipeline/DefaultResolveRequestContext.cs 50.00% 0 Missing and 1 partial ⚠️
...atures/Collections/CollectionRegistrationSource.cs 97.05% 0 Missing and 1 partial ⚠️
src/Autofac/Util/InternalTypeExtensions.cs 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1475      +/-   ##
===========================================
+ Coverage    77.98%   78.57%   +0.59%     
===========================================
  Files          211      216       +5     
  Lines         5414     5611     +197     
  Branches      1150     1207      +57     
===========================================
+ Hits          4222     4409     +187     
- Misses         692      695       +3     
- Partials       500      507       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member Author

@tillig tillig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed and iterated over this, verifying that the performance impact is negligible at best, and all the new tests pass. I'm going to call this good so I can see what it will take to update A.E.DI using the new functionality.

@tillig tillig merged commit acb2557 into develop Feb 12, 2026
10 checks passed
@tillig tillig deleted the feature/any-key branch February 12, 2026 19:02
This was referenced Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible improvements to support .NET 10 AnyKey behavior

1 participant