0
votes

I have an Azure Function that is triggered by a http request and uses bindings to output to an Azure storage queue AND return a http response.

This works when coded for dotnet-isolated, making use of the Functions.Worker assemblies. First I declare a type for both the queue message and http response:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace SmsRouter.AzFunc
{
    public class QueueAndHttpOutputType
    {
        [QueueOutput("%SendSmsQueueName%")]
        public string QueueMessage { get; set; } = "";

        public HttpResponseData HttpResponse { get; set; }
    }
}

Then I use this as the return type for the Azure Function:

[Function(nameof(SendSimpleSms))]
        public async Task<QueueAndHttpOutputType> SendSimpleSms([HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1.0/simple-sms")] HttpRequestData req,
            FunctionContext executionContext)

Unfortunately, I need to downgrade my solution to use dotnet 3.1 and the in-process model of Azure Functions due to this issue.

Does anyone know how I can achieve the same behaviour using the old style in-process Azure Function?

2
I am trying to get the similar thing working with .Net 5 with a Http trigger having a Queue as output binding. Most of the examples I see for 3.1 or lower. Will apprecaite if you can check my question stackoverflow.com/questions/69910895/… - Sugar Bowl
looks like you have an answer already :) - Rob Bowman
hving hard time finding namespace or nugget package 4 output binding [QueueOutput("xx-data")]in MultiResponse. using System.Net; using Microsoft.Azure.Functions.Worker.Extensions; using Microsoft.Azure.Functions.Worker.Http; using Azure.Storage.Queues; using Azure.Storage.Queues.Models; namespace XXX.Models { public class MultiResponse { [QueueOutput("xxx-data")] <= red squiggly under QueueOutput public string[] Messages { get; set; } public HttpResponseData HttpResponse { get; set; } }} stackoverflow.com/questions/69910895 - Sugar Bowl

2 Answers

3
votes

You can do it via injecting the ServiceBus output binding in the function itself.

public async Task<IActionResult> SendSimpleSms(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1.0/simple-sms")] HttpRequestData req,
        [Queue("%SendSmsQueueName%", Connection = "QueueConnectionString")] IAsyncCollector<string> queue
            ExecutionContext executionContext)

To add the message in service bus invoke AddAsync method as shown below

await queue.AddAsync(message);

And return the http response via return statement; something like below

return new OkObjectResult(<<Your data here>>);
0
votes

To write to the storage account queue, as opposed to a service bus queue in the accepted answer, I used the following:

[FunctionName(nameof(SendSimpleSms))]
        public async Task<IActionResult> SendSimpleSms([HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1.0/simple-sms")] HttpRequest req,
            [Queue("%SendSmsQueueName%")] IAsyncCollector<string> queue)
        {
                await queue.AddAsync(jsonString);
                ...
                return new OkObjectResult(JsonConvert.SerializeObject(serviceRefResponse));
}