5

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

1 Answer 1

6

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 HttpResponse while writing messages to the queue. Beware: browser-written, non-validated code below.

MultiResponse.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;       
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the explanation about the return type to be changed to MultiResponse. I will try it. However I am still trying to figure out how to resolve the "QueueOutput" binding issue in the MultiResponse class. I have tried including the relevant namepsaces but there is something I am missing. Please see the new image added to my edited question at the bottom. Appreicate your timely reply.
The missing nugget package was Microsoft.Azure.Functions.Worker.Extensions.Storage..just in case anyone will have issue.
These docs also suggest to include the [HttpResult] attribute on the HTTP response property of the MultiResponse, but for me it makes no difference either way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.