Fix interface interception with open generic assembly scanning (#27)#63
Merged
Conversation
EnsureInterfaceInterceptionApplies previously inspected every service exposed by the registration and threw if any was a non-interface. When scanning an assembly with AsClosedTypesOf, the concrete type is also registered as a service, causing the check to fail even when resolving the closed interface. Use the service actually being resolved (ResolveRequestContext.Service) to decide whether interface interception applies, rather than every service on the registration. The non-interface guard is preserved for direct resolution of a concrete service.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #63 +/- ##
===========================================
- Coverage 89.51% 89.34% -0.18%
===========================================
Files 2 2
Lines 124 122 -2
Branches 22 21 -1
===========================================
- Hits 111 109 -2
Misses 6 6
Partials 7 7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #27.
Problem
EnableInterfaceInterceptorsfailed when used with open generic assembly scanning:Resolving the closed interface threw an
InvalidOperationException("interface proxying only supports interface services").The validation in
EnsureInterfaceInterceptionAppliesinspected every service exposed by the registration and threw if any was a non-interface. BecauseAsClosedTypesOfscanning also registers the concrete type as a service, the check tripped on the concrete type even when the interface was the thing being resolved.Fix
Validate the service actually being resolved (
ResolveRequestContext.Service) rather than every service on the registration. This is the approach discussed on the issue years ago; it's now possible because core Autofac exposesctx.Servicein the resolve pipeline. It's also strictly less heuristic than the previous logic.The non-interface guard is preserved: directly resolving a concrete (non-interface) service registered with interface interception still throws.
Tests
New fixture
OpenGenericInterfaceInterceptionFixturewith three cases:InterceptsClosedTypesOfScannedOpenGenericInterface— the exact repro from the issue (failed before, passes now).InterceptsInterfaceServiceWhenRegistrationAlsoExposesConcreteType—AsSelf()+As<interface>(), resolved by interface, proxies correctly.ThrowsWhenResolvingConcreteServiceWithInterfaceInterception— confirms the non-interface guard still fires for direct concrete resolution.The existing
DetectsNonInterfaceServicesguard test continues to pass. Full suite green on net8.0 and net10.0; clean Release build (warnings-as-errors + analyzers).