Reputation: 1792
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.
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.
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:
Upvotes: 5
Views: 2417
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:
MultiRepsonse
MultiResponse
class something like belowMultiResponse
class in your Function like belowThis 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.
public class MultiResponse
{
[QueueOutput("myqueue-items",Connection = "AzureWebJobsStorage")]
public string[] Messages { get; set; }
public HttpResponseData HttpResponse { get; set; }
}
[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