Add the ability to annotate an API with an not supported in environment attribute #7623
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a new Roslyn analyzer (
NSBENV001) that warns developers when they use NServiceBus APIs that are not supported in specific runtime environments (e.g., Azure Functions isolated worker).Changes
Core Runtime (NServiceBus.Core)
Serverless: Identifier for Serverless environmentsenvironmentIdandreasonparametersAnalyzer (NServiceBus.Core.Analyzer)
NSBENV001constantbuild_property.NServiceBusEnvironmentMSBuild property[NotSupportedInEnvironment]is used in the configured environmentUsage
To enable the analyzer for a specific environment, add to your
.csproj:The analyzer will then warn when using APIs marked as not supported in that environment:
Transitive Package Support
The MSBuild property can be set transitively by downstream packages. For example, the Azure Functions package can automatically set this property:
This allows environment-specific packages to automatically enable the appropriate analyzer configuration without requiring users to manually configure it.
Centralized Enforcement
Organizations can centrally enforce the analyzer across all projects in their solution by setting the property in a
Directory.Build.propsfile:This ensures all projects in the solution are analyzed for the target environment without requiring individual project configuration.
Known Limitations
The analyzer analyzes direct API invocations at compile time. It will not detect usage through configuration abstraction methods defined in external libraries (e.g., a library providing helper methods that internally call forbidden APIs). This is not considered a significant limitation given modern composition patterns using the host builder APIs where endpoint configuration is typically done directly in the application code rather than through library abstractions, and the possibility to enforce it via MSBuild property centralization techniques like Directory.Build.props.
Backwards Compatibility