# Azure Functions hosting NServiceBus endpoints can be hosted in an Azure Functions app using the [`NServiceBus.AzureFunctions.AzureServiceBus`](https://www.nuget.org/packages/NServiceBus.AzureFunctions.AzureServiceBus) package. Integration is enabled in `Program.cs` by calling `AddNServiceBusFunctions`: ```cs var builder = FunctionsApplication.CreateBuilder(args); builder.AddNServiceBusFunctions(); var host = builder.Build(); await host.RunAsync(); ``` Endpoints are declared using the `[NServiceBusFunction]` attribute: ```cs public partial class OrdersEndpoint { [Function("Orders")] [NServiceBusFunction] public partial Task Orders( [ServiceBusTrigger("orders", AutoCompleteMessages = false)] ServiceBusReceivedMessage message, ServiceBusMessageActions messageActions, FunctionContext functionContext, CancellationToken cancellationToken = default); public static void ConfigureOrders(EndpointConfiguration endpointConfiguration) { endpointConfiguration.UseTransport(new AzureServiceBusServerlessTransport(TopicTopology.Default)); endpointConfiguration.UseSerialization(); endpointConfiguration.AddHandler(); } } ``` NServiceBus enabled functions must be declared in a partial class and are composed of two parts: - A partial method decorated with a `[NServiceBusFunction]` and a `[Function("MyFunction")]` attribute declaring an [Azure Service Bus trigger](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger). - The trigger must set `AutoCompleteMessages = false` since the NServiceBus pipeline takes responsibility for completing or abandoning each message based on handler outcomes; the [`NSBFUNC005` analyzer](analyzers.md) enforces this requirement. - A source generator will emit the method body that forwards each incoming message to the NServiceBus pipeline. - A static method named `Configure{FunctionName}` (flexible casing and punctuation) that configures the NServiceBus endpoint for the function. For endpoint configuration, supported transport options, and explicit handler and saga registration, etc, see [Configuration](/nservicebus/hosting/azure/functions/configuration.md). ## Hosting multiple endpoints Multiple methods decorated with `[NServiceBusFunction]` can co-exist in one Functions app. Each is registered as an independent NServiceBus endpoint with its own queue, handlers, and configure method: ```cs public partial class BillingFunctions { [Function("Invoicing")] [NServiceBusFunction] public partial Task Invoicing( [ServiceBusTrigger("billing-invoicing", AutoCompleteMessages = false)] ServiceBusReceivedMessage message, ServiceBusMessageActions messageActions, FunctionContext functionContext, CancellationToken cancellationToken = default); public static void ConfigureInvoicing(EndpointConfiguration endpointConfiguration) { endpointConfiguration.UseTransport(new AzureServiceBusServerlessTransport(TopicTopology.Default)); endpointConfiguration.UseSerialization(); endpointConfiguration.AddHandler(); } [Function("CardProcessing")] [NServiceBusFunction] public partial Task CardProcessing( [ServiceBusTrigger("billing-card-processing", AutoCompleteMessages = false)] ServiceBusReceivedMessage message, ServiceBusMessageActions messageActions, FunctionContext functionContext, CancellationToken cancellationToken = default); public static void ConfigureCardProcessing(EndpointConfiguration endpointConfiguration) { endpointConfiguration.UseTransport(new AzureServiceBusServerlessTransport(TopicTopology.Default)); endpointConfiguration.UseSerialization(); endpointConfiguration.AddHandler(); } } ``` Each endpoint has its own configure method; the source generator matches methods whose name follows the `Configure{FunctionName}` pattern, where non-alphanumeric characters in the function name are ignored. For example, `ConfigureMy_Endpoint` matches the function name `"my-endpoint"`. Endpoints share the host's service provider but maintain independent message-handling pipelines. ## Send-only endpoints A [send-only endpoint](/nservicebus/endpoints/index.md#send-only) can be declared for components that need to dispatch messages without receiving incoming messages. Send-only endpoints can be used, for example, from a `TimerTrigger` or an `HttpTrigger` function serving as an HTTP API: ```cs [NServiceBusSendOnlyFunction("client")] public static void ConfigureClient(EndpointConfiguration endpointConfiguration, IServiceCollection services) { services.AddSingleton(new MyComponent("client")); var transport = new AzureServiceBusServerlessTransport(TopicTopology.Default); var routing = endpointConfiguration.UseTransport(transport); routing.RouteToEndpoint(typeof(SubmitOrder), "sales"); endpointConfiguration.UseSerialization(); } ``` The attribute name becomes the [keyed-services](https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection#keyed-services) key used to resolve `IMessageSession` and any endpoint-scoped services. Set the optional `Connection` property to override the default connection setting name. Send-only endpoints are discovered and registered automatically by `builder.AddNServiceBusFunctions()`. ```cs class SalesApi([FromKeyedServices("client")] IMessageSession session, [FromKeyedServices("client")] MyComponent component) { [Function("SalesApi")] public async Task Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData request) { await session.Send(new SubmitOrder()); var response = request.CreateResponse(HttpStatusCode.OK); await response.WriteStringAsync(component.EndpointName); return response; } } ```