Erik Z
Erik Z

Reputation: 4780

How to send message to message bus from Http triggered function?

I'm using ServiceBus.Extensions 5.7.0 and have a Azure function triggered by a HttpTrigger. From this function I want to send a message to a topic on my Azure Message Bus. How can this be done? My function has to return HttpResponseData as a respone to the http request. I can't use the ServiceBusOutput attribute either since it's not allowed to be used on parameters out from my function.

Upvotes: 0

Views: 105

Answers (1)

Sean Feldman
Sean Feldman

Reputation: 26057

In Isolated Worker SDK this scenario is called a Multi-output scenario. You'll need to return a POCO, with properties, where one property that will be mapped to HTTP response and another to the Azure Service Bus entity.

public class MyOutputType
{
   public HttpResponseData HttpResponse { get; set; } // HTTP response [ServiceBusOutput(queueOrTopicName: "dest", Connection = "AzureServiceBus")]
   public string Message { get; set; } // message payload
}

And in your function, you'd return an instance of MyOutputType.

[Function("MultiOutput")]
public static MyOutputType Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req,
            FunctionContext context)
 {
   var response = req.CreateResponse(HttpStatusCode.OK);
   response.WriteString("Success!");

   var myQueueOutput = "Payload for ASB";

    return new MyOutputType
    {
       Message = myQueueOutput,
       HttpResponse = response
    };
 }

Upvotes: 2

Related Questions