Custom Azure Functions triggers

If the trigger function must be customized, disable generation of the trigger function by removing the NServiceBusTriggerFunction attribute. A custom trigger function can then be added manually to the project:

class CustomTriggerDefinition
{
    IFunctionEndpoint functionEndpoint;

    public CustomTriggerDefinition(IFunctionEndpoint functionEndpoint)
    {
        this.functionEndpoint = functionEndpoint;
    }

    [FunctionName("MyCustomTrigger")]
    public Task Run(
        [ServiceBusTrigger("MyFunctionsEndpoint")]
        ServiceBusReceivedMessage message,
        ServiceBusClient client,
        ServiceBusMessageActions messageActions,
        ILogger logger,
        ExecutionContext executionContext)
    {
        return functionEndpoint.ProcessAtomic(message, executionContext, client, messageActions, logger);
    }
}

Configuring transaction mode

If the NServiceBusTriggerFunction attribute is not used, IFunctionEndpoint.Process will determine the transaction mode based on the ServiceBusTrigger's auto-complete property:

If auto-complete is enabled, which is the default, NServiceBus can't control the receive transaction and the message is processed in TransportTransactionMode.ReceiveOnly mode.

[FunctionName("ProcessMessage")]
public async Task Run(
    // Setting AutoComplete to true (the default) processes the message non-transactionally
    [ServiceBusTrigger("ProcessMessage", AutoCompleteMessages = true)]
    ServiceBusReceivedMessage message,
    ILogger logger,
    ExecutionContext executionContext)
{
    await endpoint.ProcessNonAtomic(message, executionContext, logger);
}

If auto-complete is disabled, NServiceBus can fully control incoming and outgoing messages and the message is processed in TransportTransactionMode.SendsAtomicWithReceive mode.

[FunctionName("ProcessMessageTx")]
public Task RunTx(
    // Setting AutoComplete to false processes the message transactionally
    [ServiceBusTrigger("ProcessMessageTx", AutoCompleteMessages = false)]
    ServiceBusReceivedMessage message,
    ServiceBusClient client,
    ServiceBusMessageActions messageActions,
    ILogger logger,
    ExecutionContext executionContext)
{
    return endpoint.ProcessAtomic(message, executionContext, client, messageActions, logger);
}
Incorrectly configuring the service bus trigger auto-complete setting can lead to message loss. Use the auto-detection mechanism on the function endpoint interface, or use the trigger function attribute to specify message consistency.

Related Articles


Last modified