﻿# Custom Azure Functions triggers


> [!WARNING]
> **NServiceBus.AzureFunctions.InProcess.ServiceBus component has been sunset**
>
> Microsoft announced that .NET 8 will be [the last release supporting the in-process hosting model](https://techcommunity.microsoft.com/t5/apps-on-azure-blog/net-on-azure-functions-august-2023-roadmap-update/ba-p/3910098).
> We will continue to provide support and address critical fixes during the sunset period, but no new features will be added.
> New projects should use the [isolated worker model](/nservicebus/hosting/azure-functions-service-bus/index.md) instead. Follow [these instructions](/nservicebus/upgrades/azure-functions-service-bus-in-process-isolated-worker.md) to migrate.
> For information on the date when this component will stop receiving any update check our [support policy page](/nservicebus/upgrades/all-versions.md#host-packages-nservicebus-azurefunctions-inprocess-servicebus).


To configure a custom trigger function, remove the native `NServiceBusTriggerFunction` attribute. A custom trigger function can then be added manually to the project:

<!-- snippet: custom-trigger-definition -->

```cs
class CustomTriggerDefinition(IFunctionEndpoint 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);
    }
}
```

<!-- endsnippet -->

## Configuring transaction mode

To use the `TransportTransactionMode.SendsAtomicWithReceive` mode, `AutoCompleteMessages` needs to be **disabled** and the custom trigger needs to call `ProcessAtomic`.

<!-- snippet: asb-function-message-consistency-process-transactionally -->

```cs
[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);
}
```

<!-- endsnippet -->

To use the `TransportTransactionMode.ReceiveOnly` mode, `AutoCompleteMessages` needs to be **enabled** and the trigger needs to call `ProcessNonAtomic`.

<!-- snippet: asb-function-message-consistency-process-non-transactionally -->

```cs
[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);
}
```

<!-- endsnippet -->

> [!WARNING]
> Incorrectly configuring the service bus trigger `AutoCompleteMessages` 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.
