Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 16, 2025

Introduces a Roslyn analyzer and code fix to detect and correct improper feature enablement patterns in NServiceBus features.

Problem:
Using EnableFeature<TFeature>() inside the Defaults method violates NServiceBus best practices. Dependent features should be enabled via the protected Enable<TFeature>() method in the constructor instead.

Changes:

  • Added FeatureDefaultsEnableFeatureAnalyzer (NSB0019) to detect EnableFeature<TFeature>() calls within Defaults lambdas
  • Added FeatureDefaultsEnableFeatureFixer to automatically move these calls to the constructor using Enable<TFeature>()
  • Optimized analyzer performance with syntax-based filtering before semantic model lookups
  • Supports FixAll provider for batch corrections across solution

Example:

// Before (triggers NSB0019)
class MyFeature : Feature
{
    public MyFeature()
    {
        Defaults(settings => settings.EnableFeature<DependencyFeature>());
    }
}

// After (code fix applied)
class MyFeature : Feature
{
    public MyFeature()
    {
        Enable<DependencyFeature>();
    }
}

Scoped out:

  • Local functions or static methods invoked within Defaults

💡 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] Add analyzer to enforce feature enablement best practices Analyzer: Error when Feature Defaults are used to enable other features Nov 16, 2025
Copilot AI requested a review from danielmarbach November 16, 2025 13:58
@danielmarbach danielmarbach deleted the copilot/sub-pr-7468 branch November 16, 2025 14:00
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