Publish message in Http triggered Azure Function

Hi,

We have an Http triggered Azure Function that should publish a message to a topic on an Azure ServiceBus queue and also handle the reply message (which is sent from a different Azure Function app).

Here is a very simplified example:

// Program.cs
[assembly:NServiceBusTriggerFunction("test-endpoint")]

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddSingleton<MessageSenderService>();
    })
    .UseNServiceBus(configuration =>
    {
        configuration.Transport.Topology = TopicTopology.Single("test-topic");
    })
    .Build();
    
await host.RunAsync();
public class TestTriggerFunction(MessageSenderService messageSenderService, 
    ILogger<TestTrigger> logger)
{
    [Function("TestTrigger")]
    public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
    {
        logger.LogInformation("C# HTTP trigger function processed a request.");
        messageSenderService.PublishMessage();
        return new OkObjectResult("Message is published.");
    }
}

public class Messages
{
    public class OutgoingMessage : IMessage
    {
        public string? Content { get; set; }
    }
    
    public class ReplyMessage : IMessage
    {
        public string? Content { get; set; }
    }
}

public class MessageSenderService
{
    public void PublishMessage()
    {
        var outgoingMessage = new Messages.OutgoingMessage
        {
            Content = "Hello from NServiceBus!"
        };
        // Publish to topic somehow
    }
}

public class ReplyMessageHandler(ILogger<ReplyMessageHandler> logger) : 
    IHandleMessages<Messages.ReplyMessage>
{
    public Task Handle(Messages.ReplyMessage message, IMessageHandlerContext context)
    {
        logger.LogInformation($"Received ReplyMessage with content: {message.Content}");
        return Task.CompletedTask;
    }
}

With this simplified example the easy answer is probably to inject IFunctionEndpoint into MessageSenderService and pass a FunctionContext from the Http triggered function to the PublishMessage method.

However, in our real world example the service publishing the message is nested deep within our app in a separate project; the Http triggered function sends a command to MediatR, where the handler then calls a method in another service to publish the message.
It seems a bit messy to pass the FunctionContext all the way through this chain. Is there any elegant way to publish a message when it is not done directly from the function?

We have looked into using IMessageSession, but have not been able to get it resolved with Dependency Injection - tips on how to register this in Program.cs are appreciated.

@lena,
The IFunctionEndpoint requires the FunctionContext to send messages properly, so you’ll have to pass it into your message send service one way or another.

If you do not want to do this, you can alternatively create a Send-Only endpoint on-the-fly within your message service, but this can bring other complications. See Azure Function Hosted Send Only Endpoint. Be aware the code shown in the link is specifically for the In-Process model, not the Worker model.