Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 19, 2025

This PR introduces an explicit handler registration API for NServiceBus, replacing automatic handler discovery with compile-time verified, developer-controlled registration.

Summary of Changes

Core API Changes

  • New explicit registration: config.AddHandler<THandler>() replaces automatic scanning
  • Handler execution order: Determined by registration order, replacing ExecuteTheseHandlersFirst()
  • Breaking change: ExecuteTheseHandlersFirst() marked obsolete (error-level)

Architecture

  • Source generator: AddHandlerInterceptor generates optimized registration code at compile time
  • Type-safe invocation: Generic MessageHandlerInvoker<THandler, TMessage> replaces reflection-based dispatch
  • Registry enhancements: Strongly-typed registration methods with deduplication
    • AddMessageHandlerForMessage<THandler, TMessage>()
    • AddTimeoutHandlerForMessage<THandler, TMessage>()

Analyzer (NSB0020)

  • Prevents injection of IMessageSession/IEndpointInstance into handlers
  • Enforces use of IMessageHandlerContext parameter

Message Type Discovery

  • Separated handler scanning from message type discovery
  • Message types now registered from both conventions and handler interfaces
  • Handles message hierarchy correctly when base types not in EnclosedMessageTypes header

Migration Example

Before:

config.ExecuteTheseHandlersFirst(typeof(FirstHandler), typeof(SecondHandler));

After:

config.AddHandler<FirstHandler>();
config.AddHandler<SecondHandler>();

Impact

  • Compile-time handler verification
  • Explicit control over handler order and registration
  • Performance improvements via source-generated registration
  • Breaking change requiring code migration

PR Stats: 73 commits, 66 files, +1,481/-927 lines
Target: vNext (v11.0.0+)

Original prompt

Summarize the changes in #7439


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix null reference exception in message handler Summarize changes in PR #7439: Explicit AddHandler registration API Nov 19, 2025
Copilot AI requested a review from andreasohlund November 19, 2025 13:49
@andreasohlund andreasohlund deleted the copilot/fix-null-reference-exception branch November 19, 2025 13:56
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.

2 participants