Sugar Bowl
Sugar Bowl

Reputation: 1792

Http trigger function with output binding to queue

I have a http triggered function in .Net5, isolated function.

I am having hard time getting the output binding working for this https function.

The https function retrieves a list of objects. These objects need to be added as separate messages to the queue.

[FunctionName("TestQueueOutput")]
[return: Queue("myqueue-items", Connection = "AzureWebJobsStorage")]    
public async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,          
            FunctionContext context)
{
    HttpResponseData okResponse = null; 
    okResponse = req.CreateResponse(System.Net.HttpStatusCode.OK);     

    // List of Objects
    var _list= await _repo.Get();
    await okResponse.WriteAsJsonAsync(_list);
    return okResponse;       
}

When I run the function, http response can see the list but nothing in the queue defined in Azure.

I further followed the following article for adding output binding to Http trigger.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-add-output-binding-storage-queue-vs?tabs=in-process

When I add the Multiresponse class for Isolated process, the output binding for the QueueOutput, the function gives red squiggly lines for it. Can't find the correct nugget package to fix it.

Output binding

I spent countless hours to get the list items added to the queue as messages.

I am doing it right? Any guidance will be appreciated

Update #1: when I added the MultiResponse class, I can't figure out how to resolve the QueueOutput issue as in the following image:

QueueOutput Binding in MultiResponse class

Upvotes: 5

Views: 2417

Answers (1)

rickvdbosch
rickvdbosch

Reputation: 15551

There are a couple of things you need to take a look at. For one, you expect to return HttpResponseData, but also have a return binding setup. And, as the HttpResponseData expects, you're returning a HttpResponse. But the returns binding is trying to translate that into a queue message. And the message information seems to be written to the response body, which is not how it should work.

As per the article you linked, try the following:

  1. Add a class to your project, calling it MultiRepsonse
  2. Implement the MultiResponse class something like below
  3. Use the MultiResponse class in your Function like below

This implements the MultiResponse, tells the Function to use it and enables you to return a HttpResonse while writing messages to the queue. Beware: browser-written, non-validated code below.

MultiRespsonse.cs

public class MultiResponse
{
    [QueueOutput("myqueue-items",Connection = "AzureWebJobsStorage")]
    public string[] Messages { get; set; }

    public HttpResponseData HttpResponse { get; set; }
}

TestQueueOutput.cs

[FunctionName("TestQueueOutput")]
public async Task<MultiResponse> RunAsync(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
    FunctionContext context)
{
    var response = new MultiResponse();
    response.HttpResponse = req.CreateResponse(HttpStatusCode.OK);

    var _list = await _repo.Get();
    response.Messages = new string[_list.Count];
    foreach (var item in _list)
    {
        // Add the item to the list
    }

    return response;       
}

Upvotes: 6

Related Questions