2

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?

3
  • 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/… Commented Nov 10, 2021 at 9:46
  • looks like you have an answer already :) Commented Nov 10, 2021 at 10:31
  • 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 Commented Nov 11, 2021 at 1:07

2 Answers 2

3

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

4 Comments

Thanks for your answer but I need to publish to an Azure Storage Account Queue rather than Service Bus Queue.
In that case you can use Queue output binding as [Queue("%SendSmsQueueName%", Connection = "QueueConnectionString")] IAsyncCollector<CloudQueueMessage> queue ExecutionContext executionContext). You can also use out T also to write single message, for mulitple messages use IAsyncCollector. More information at here
Thanks again but in my case the Azure Function needs to return a http response as well as publish a single message to a storage queue. I think your original answer put me on the right track, to return an IActionResult and use queue.AddAsync.
Yes, my last comment is related to change the [ServiceBus to [Queue binding.... Line #3 as per my answer. Now to add the message use, await queue.AddAsync(message);
0

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));
}

Comments

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.