Skip to content

Analyzer warnings for static handler classes#7663

Merged
danielmarbach merged 1 commit into
masterfrom
static_classes
Mar 20, 2026
Merged

Analyzer warnings for static handler classes#7663
danielmarbach merged 1 commit into
masterfrom
static_classes

Conversation

@danielmarbach
Copy link
Copy Markdown
Contributor

@danielmarbach danielmarbach commented Mar 19, 2026

This pull request introduces a new analyzer diagnostic to prevent static classes from being used as handler classes in NServiceBus, ensuring that only non-static classes are allowed for handler registration via the [Handler] attribute or convention-based mechanisms. The changes add enforcement in both the analyzer logic and the code generation pipeline, as well as new tests to verify the behavior.

Analyzer and Diagnostic Improvements:

  • Added a new diagnostic ID NSB0038 (HandlerClassCannotBeStatic) to identify and report static handler classes. [1] [2]
  • Updated HandlerAttributeAnalyzer to report the new diagnostic when a static class is decorated with the [Handler] attribute or detected as a convention-based handler. [1] [2] [3]

Code Generation and Parsing Adjustments:

  • Modified the handler registration generator to exclude static classes from being considered for handler generation.
  • Updated the handler parser to skip static classes during interception logic.

Testing Enhancements:

  • Added and updated tests to assert that static handler classes are correctly reported with the new diagnostic, ensuring coverage for both attribute-based and convention-based handlers. [1] [2]

@danielmarbach danielmarbach merged commit 25c822e into master Mar 20, 2026
7 checks passed
@danielmarbach danielmarbach deleted the static_classes branch March 20, 2026 07:00
@danielmarbach danielmarbach added this to the 10.2.0 milestone Apr 1, 2026
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