Azure Function Hosted Send Only Endpoint

It appears that there is no straight forward way to host a send only endpoint in an Azure Function.

All implementations in the samples expect you to use ServiceBusTriggeredEndpointConfiguration . It can’t be used in an NSB send only endpoint and the IFunctionEndpoint expects it so you cant use the generic host builder.

The examples on this page don’t work with SendOnly enabled and IFunctionsHostBuilder doesn’t work with generic host.

Azure Functions with Azure Service Bus • NServiceBus.AzureFunctions.InProcess.ServiceBus • Particular Docs

Since it’s going to be a send-only endpoint, it’s not going to be a “functions endpoint.” The overloading of the word endpoint here is unfortunate, but it’s more like you’re going to have a library sending messages as part of an HTTP API.

So, you’re not going to use any of the NServiceBus Functions libraries here at all. You’re just setting up a send-only endpoint in a curious hosting environment.

But…you can’t really use the .UseNServiceBus(…) stuff either, because that’s an extension method on normal webapp hosting types, and this isn’t a normal webapp. Instead you have to do it the old-fashioned way.

Here’s what I mocked up:

[assembly: FunctionsStartup(typeof(Startup))]

class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.Add(new ServiceDescriptor(typeof(IMessageSession), provider =>
        {
            var cfg = new EndpointConfiguration("SendOnly");
            cfg.SendOnly();

            cfg.UseTransport<AzureServiceBusTransport>();
            // set connection string, etc.

            // Unfortunately, no async/await here
            var endpoint = Endpoint.Start(cfg).GetAwaiter().GetResult();

            return endpoint;
        }, ServiceLifetime.Singleton));
    }
}

Then you can make the HTTP function class non-static and use constructor injection to get your IMessageSession.

This snippet would make a good example for others wondering the same and looking for how to use NSB to convert HTTP calls into messages. Especially if running into the early preview blog post that showed the API that is now not available.

1 Like