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.

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,

        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
    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.