Getting Started
Architecture
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

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

To use the TransportTransactionMode.SendsAtomicWithReceive mode the auto-complete needs to be disabled and the trigger needs to call ProcessAtomic.

[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);
}

To use the TransportTransactionMode.ReceiveOnly mode the auto-complete needs to be enabled and the trigger needs to call ProcessNonAtomic.

[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);
}

Related Articles