Skip to content

fix: ServiceInterceptor AllowMultiple not respected due to .Distinct()#345

Closed
liuhaoyang wants to merge 57 commits into
masterfrom
fix/service-interceptor-allow-multiple
Closed

fix: ServiceInterceptor AllowMultiple not respected due to .Distinct()#345
liuhaoyang wants to merge 57 commits into
masterfrom
fix/service-interceptor-allow-multiple

Conversation

@liuhaoyang

Copy link
Copy Markdown
Member

Problem

ServiceInterceptorAttribute declares AllowMultiple = true, but multiple ServiceInterceptorAttribute instances wrapping the same inner interceptor type were being incorrectly deduplicated.

Root Cause

In InterceptorCollector.Collect(), the pipeline ends with:

HandleSort().HandleMultiple()).Distinct().ToArray()
  • HandleMultiple() correctly keeps AllowMultiple interceptors and deduplicates non-AllowMultiple ones by type.
  • However, the trailing .Distinct() uses Equals() for deduplication.
  • ServiceInterceptorAttribute.Equals() compares by _interceptorType, so two instances wrapping the same type are treated as equal.
  • Result: ServiceInterceptorAttribute.AllowMultiple = true was effectively ignored.

Fix

Removed .Distinct() since HandleMultiple() already provides the correct deduplication semantics:

  • AllowMultiple = true → all instances kept
  • AllowMultiple = false → deduplicated by type

Changes

File Change
src/AspectCore.Core/DynamicProxy/InterceptorCollector.cs Remove .Distinct() from the Collect pipeline
tests/AspectCore.Tests/Integrate/ServiceInterceptorTests.cs Updated existing test to expect both interface and class-level [ServiceInterceptor] to execute (2 instead of 1); added test for single-attribute case

Test Results

  • All 225 tests pass across net6.0/net7.0/net8.0/net9.0
  • Zero regressions

Fixes #325

huoshan12345 and others added 26 commits July 6, 2026 21:39
Remove .Distinct() from InterceptorCollector.Collect() because HandleMultiple()
already correctly handles deduplication:
- Interceptors with AllowMultiple=true are kept as-is (no dedup)
- Interceptors with AllowMultiple=false are deduplicated by type

The .Distinct() call was incorrectly deduplicating ServiceInterceptorAttribute
instances that wrap the same inner interceptor type, because ServiceInterceptorAttribute.Equals()
compares by _interceptorType. This caused ServiceInterceptor to effectively ignore AllowMultiple=true.

Fixes #325
@liuhaoyang liuhaoyang added the bug label Jul 12, 2026
@liuhaoyang

Copy link
Copy Markdown
Member Author

关闭此 PR,因为它错误地包含了 PR #334 的变更(分支基于错误的 base 创建)。已重新创建干净的 PR。

@liuhaoyang liuhaoyang closed this Jul 12, 2026
@liuhaoyang liuhaoyang deleted the fix/service-interceptor-allow-multiple branch July 12, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ServiceInterceptor 不支援 AllowMultiple

2 participants