Skip to content

Fix interface interception with open generic assembly scanning (#27)#63

Merged
tillig merged 1 commit into
developfrom
feature/issue-27
Jun 23, 2026
Merged

Fix interface interception with open generic assembly scanning (#27)#63
tillig merged 1 commit into
developfrom
feature/issue-27

Conversation

@tillig

@tillig tillig commented Jun 23, 2026

Copy link
Copy Markdown
Member

Fixes #27.

Problem

EnableInterfaceInterceptors failed when used with open generic assembly scanning:

builder
  .RegisterAssemblyTypes(typeof(Program).Assembly)
  .AsClosedTypesOf(typeof(ICommandHandler<,>))
  .EnableInterfaceInterceptors()
  .InterceptedBy(typeof(LoggingInterceptor));

Resolving the closed interface threw an InvalidOperationException ("interface proxying only supports interface services").

The validation in EnsureInterfaceInterceptionApplies inspected every service exposed by the registration and threw if any was a non-interface. Because AsClosedTypesOf scanning 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 exposes ctx.Service in 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 OpenGenericInterfaceInterceptionFixture with three cases:

  • InterceptsClosedTypesOfScannedOpenGenericInterface — the exact repro from the issue (failed before, passes now).
  • InterceptsInterfaceServiceWhenRegistrationAlsoExposesConcreteTypeAsSelf() + As<interface>(), resolved by interface, proxies correctly.
  • ThrowsWhenResolvingConcreteServiceWithInterfaceInterception — confirms the non-interface guard still fires for direct concrete resolution.

The existing DetectsNonInterfaceServices guard test continues to pass. Full suite green on net8.0 and net10.0; clean Release build (warnings-as-errors + analyzers).

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.
@tillig tillig merged commit e63ef5c into develop Jun 23, 2026
12 checks passed
@tillig tillig deleted the feature/issue-27 branch June 23, 2026 23:22
@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.34%. Comparing base (8f1268b) to head (12742a9).
⚠️ Report is 2 commits behind head on develop.

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.
📢 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.

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.

Interface interception fails when used with open generic assembly scanning

1 participant