Skip to content

Add QueueException and guard against reusing behavior instances#448

Merged
niemyjski merged 1 commit intomainfrom
feature/queue-behavior-attach-guard
Feb 6, 2026
Merged

Add QueueException and guard against reusing behavior instances#448
niemyjski merged 1 commit intomainfrom
feature/queue-behavior-attach-guard

Conversation

@niemyjski
Copy link
Member

Summary

  • Add QueueException following the established pattern (CacheException, StorageException, MessageBusException) to provide a consistent exception type for queue-specific errors
  • Add a guard in QueueBehaviorBase<T>.Attach() that throws QueueException if the behavior is already attached to a queue, preventing subtle bugs from duplicate event handler subscriptions or stale _queue references when reusing a single behavior instance across multiple queues
  • Document behavior attachment rules and QueueException in docs/guide/queues.md

Changes

File Description
src/Foundatio/Queues/QueueException.cs New exception class with message and message + innerException constructors
src/Foundatio/Queues/QueueBehaviour.cs ArgumentNullException guard + QueueException if already attached
docs/guide/queues.md "Behavior Attachment Rules" subsection + "Queue Exceptions" section
tests/.../InMemoryQueueTests.cs AttachBehavior_WhenAlreadyAttached_ThrowsQueueException and AttachBehavior_WithNullQueue_ThrowsArgumentNullException

Test plan

  • AttachBehavior_WhenAlreadyAttached_ThrowsQueueException — verifies the guard throws when reusing a behavior across two queues
  • AttachBehavior_WithNullQueue_ThrowsArgumentNullException — verifies null argument guard
  • Full test suite passes (1783 passed, 1 pre-existing flaky failure in CanQueueAndDequeueMultipleWorkItemsAsync)

Add QueueException following the established pattern (CacheException, StorageException, MessageBusException) to provide a consistent exception type for queue operations.

Add a guard in QueueBehaviorBase.Attach that throws QueueException if the behavior is already attached to a queue, preventing subtle bugs from duplicate event handler subscriptions or stale queue references.

Include documentation updates in docs/guide/queues.md with behavior attachment rules and QueueException usage, plus two new tests following naming standards.

Co-authored-by: Cursor <cursoragent@cursor.com>
@niemyjski niemyjski requested review from Copilot and ejsmith and removed request for Copilot February 6, 2026 21:09
@niemyjski niemyjski self-assigned this Feb 6, 2026
@niemyjski
Copy link
Member Author

In exceptionless and other apps I've seen usages of where behaviors are created via new instances on queues. But I do see Behaviors = container.GetServices<IQueueBehavior<T>>().ToList() but I don't see anywhere where those were registered anywhere in any codebase. Also, I'd think those would be registered as transient if they were. @ejsmith

@niemyjski niemyjski merged commit e0842d6 into main Feb 6, 2026
4 checks passed
@niemyjski niemyjski deleted the feature/queue-behavior-attach-guard branch February 6, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants