Multiple Binding in Azure Function (both In process and Out of Process)

In this post, we will look into how to support multiple output binding in an Azure Function. We will attempt to explore how to support multiple output both in in process functions as well as out of process functions.

Multiple output binding are useful when you have a scenario where you want to response to an HTTP Trigger with an Http Message and at the same time, add a new Item to the Queue. This is handled in difference ways in the in-process functions and out-of-process functions.

In Process Functions

In the conventional In process Azure functions, the function is a class library that run in the same process as the Host Process. With the in process functions, you could easily support multiple output binding by marking the ouput parameter as out.

For example, to support a Queue output along with the Http Response, the following could be done.

[FunctionName("MultioutputSample")]
public static IActionResult MultiOutputDemo([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
    ILogger log,[Queue("SampleQueue")]out string queueOutput)
{

    queueOutput = "New Activity detected";
    return new OkObjectResult($"Hello, Welcome to Isolated functions demo.");
}

In the above example, a string “Hello, Welcome to Isolated functions demo.” would be send back as Http Response, while another string “New Activity detected” is added to the “SampleQueue” that has been marked with the queueOutput out parameter.

Out Of Process Isolated Functions

The out of process isolated functions works as console application and runs independed from the Azure Function Host. As a developer, you are responsible for hosting your own IHost instance.

The value returned by the isolated function is bound to the output binding. This would mean the out of process isolated function has to use another approach for support multiple output. This is done defining a Custom Type and having a property marked with the QueueOutput attribute. For example,

[Function(nameof(MultiOutputSample))]
        public static async Task<CustomOutputType> MultiOutputSample(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
            FunctionContext executionContext)
{
    // Use the FunctionContext.GetLogger to fetch instance of ILogger
    var logger = executionContext.GetLogger(nameof(StaticFunctions));
    logger.LogInformation("Started execution of function");

    var data = await req.ReadFromJsonAsync<UserDto>();

    // Should use HttpResponseData as response
    var response = req.CreateResponse(HttpStatusCode.OK);
    await response.WriteStringAsync($"Hello {data.Name}, Welcome to Isolated functions demo.");


    return new CustomOutputType
    {
        UserResponse = response,
        UserTask = $"{data.Name} have logged in"
    };
}

Where the CustomOutputType is defined as

public class CustomOutputType
{
    [QueueOutput("SampleQueue")]
    public string UserTask { get; set; }
    public HttpResponseData UserResponse { get; set; }
}

In the above defined CustomOutput, the UserResponse being of type HttpResponseData would return a http message back to the caller, while at the same time, the string in the UserTask is added to the SampleQueue.

As seen, both in process and out of process Azure Functions supports multiple output binding, but uses different approaches for achieving the results. Both approaches are pretty easy to use.

Advertisement

One thought on “Multiple Binding in Azure Function (both In process and Out of Process)

  1. HI,

    I am trying to implment the multiple binding on an http function. I have done exactly defined in your this article. However CustomOutputType class when I put the output binding ” [QueueOutput(“SampleQueue”)]” I get squiglly lines under QueueOutput. My class is as follows:
    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”)]
    public string[] Messages { get; set; }
    public HttpResponseData HttpResponse { get; set; }
    }
    }
    Please see my question on SO:
    https://stackoverflow.com/questions/69910895/

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s