Getting Started
Architecture
NServiceBus
Transports
Persistence
Hosting
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Modernization
Samples

Roslyn analyzers for Azure Functions

The package includes Roslyn analyzers that enforce required patterns for Azure Functions hosting.

Diagnostics

IDSeverityDescription
NSBFUNC001ErrorA class containing a method with [NServiceBusFunction] must be partial.
NSBFUNC002WarningA function class should not implement IHandleMessages<T>. Register handlers separately in endpoint configuration.
NSBFUNC003ErrorA method with [NServiceBusFunction] must be partial.
NSBFUNC004ErrorOnly one Configure{FunctionName} method is allowed for a function.
NSBFUNC005ErrorThe Service Bus trigger must explicitly set AutoCompleteMessages = false.
NSBFUNC006ErrorThe function method is invalid, for example because required trigger parameters are missing or the matching Configure{FunctionName} method is missing or invalid.
NSBFUNC007ErrorUnsupported endpoint configuration API is used in a Configure… method for a receiving or send-only endpoint.
NSBFUNC008ErrorUnsupported SendOptions or ReplyOptions API is used for Azure Functions endpoints.
NSBFUNC009ErrorEndpointConfiguration.UseTransport(…) does not use AzureServiceBusServerlessTransport.
NSBFUNC010ErrorA method marked with [NServiceBusSendOnlyFunction] is not a valid send-only endpoint declaration.

Unsupported endpoint configuration APIs

Because the Azure Functions runtime is responsible for fetching messages from the broker, some endpoint configuration APIs are not supported in this hosting model. NSBFUNC007 is reported when a Configure… method uses one of these unsupported APIs.

Unsupported APIs include:

  • EndpointConfiguration.PurgeOnStartup
  • EndpointConfiguration.LimitMessageProcessingConcurrencyTo
  • EndpointConfiguration.DefineCriticalErrorAction
  • EndpointConfiguration.SetDiagnosticsPath
  • EndpointConfiguration.MakeInstanceUniquelyAddressable
  • EndpointConfiguration.UniquelyIdentifyRunningInstance
  • EndpointConfiguration.OverrideLocalAddress

For send-only endpoints, some of these APIs are invalid because send-only endpoints do not receive messages.

Unsupported send and reply options APIs

NSBFUNC008 is reported when code uses APIs that try to route to a specific instance, which is not supported for Azure Functions because instances are ephemeral.

Unsupported APIs:

  • SendOptions.RouteToThisInstance
  • ReplyOptions.RouteReplyToThisInstance

Required transport

NSBFUNC009 ensures EndpointConfiguration.UseTransport(…) uses AzureServiceBusServerlessTransport.

For the supported transport configuration in this hosting model, see Azure Functions configuration.

Send-only endpoint validation

NSBFUNC010 is reported when a method marked with [NServiceBusSendOnlyFunction] does not match the required shape.

The method must:

  • be static
  • be named Configure{EndpointName} (case-insensitive)
  • take EndpointConfiguration as the first parameter
  • only use IServiceCollection, IConfigurationManager (or any interface it implements, such as IConfiguration or IConfigurationBuilder), IHostEnvironment, and IDictionary<object, object> as additional optional parameters

Code fixes

The package currently includes one code fix.

NSBFUNC006

The code fix can:

  • add missing trigger-related parameters such as FunctionContext and CancellationToken
  • add missing additional trigger parameters required by the function signature
  • generate a missing Configure{FunctionName} method stub