Search code examples
c#azure-functionsqueuetriggerqueue-table

Return Table Data in Queue Trigger function


I have been trying to return Table Storage rows back in Queue Trigger Function URL, But could not getting success. Below is my code.

public static async Task<List<PatchesList>> Run([QueueTrigger("send-patches-list", Connection = 
"AzureWebJobsStorage")]string myQueueItem, [Table(tableName: "getpatcheslist", Connection = 
"AzureWebJobsStorage")]CloudTable cloudTable, ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

    TableQuery<PatchesList> projectionQuery = new TableQuery<PatchesList>().Select(
new string[] { "RowKey", "Name" });

    var grouprows = await cloudTable.ExecuteQuerySegmentedAsync(projectionQuery, null);
    List<PatchesList> groupslist = new List<PatchesList>();
    log.LogInformation($"C# Queue trigger function processed: {grouprows}");
    foreach (var c in grouprows.Results)
    {
        groupslist.Add(new PatchesList
        {
            RowKey = c.RowKey,
            Name = c.Name
        });
        log.LogInformation($"C# Queue trigger function processed: {groupslist[0].Name}");
    }

    return groupslist;
}

public class PatchesList : TableEntity
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Name { get; set; }
}

I am having trouble sending data back and Is this approach feasible, can Queue Trigger send back response?


Solution

  • Queue-triggered Azure Functions don't have an http function URL and have output bindings, not returns. If you need to return a JSON version of your objects, then you need to use an HTTP-triggered function (which can return HTML/JSON/etc to the caller), not a queue. If you must use a queue, then you need to output your data to some other source using a binding or custom code that you create. If you explain your use case/needs a bit more, it may be possible to elaborate on a solution.

    Update:

    So based on your comment, you want to take your groupsList and send it to an HTTP endpoint (a URL). If that is correct, then you just need to use HttpClient to POST the data to that URL...no output binding.

    First, for functions, it's best practice to use a static copy of HttpClient (see https://learn.microsoft.com/en-us/azure/azure-functions/manage-connections), so you would add the following line above your function:

    private static HttpClient httpClient = new HttpClient();
    

    Then use that httpClient to POST your data as JSON:

    var json = JsonConvert.SerializeObject(groupsList);
    var content = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
    var response = await httpClient.PostAsync(<yourUrl>, content);
    

    You can inspect the response if you want to verify success and even use a 3rd party library such as Polly to retry requests should they fail. Once the POST is complete, you're done. No return or output binding from the Function, since it technically has no output.