Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind output values to my async Azure Function?

Tags:

How can I bind my outputs to an async function? The usual method of setting the parameter to out doesn't work with async functions.

Example

using System;

public static async void Run(string input, TraceWriter log, out string blobOutput)
{
    log.Info($"C# manually triggered function called with input: {input}");
    await Task.Delay(1);

    blobOutput = input;
}

This results in a compilation Error:

[timestamp] (3,72): error CS1988: Async methods cannot have ref or out parameters

Binding used (fyi)

{
  "bindings": [
    {
      "type": "blob",
      "name": "blobOutput",
      "path": "testoutput/{rand-guid}.txt",
      "connection": "AzureWebJobsDashboard",
      "direction": "out"
    },
    {
      "type": "manualTrigger",
      "name": "input",
      "direction": "in"
    }
  ],
  "disabled": false
}
like image 999
Zain Rizvi Avatar asked Nov 03 '16 19:11

Zain Rizvi


People also ask

Can Azure function return a value?

Using the Azure Function return valueIn languages that have a return value, you can bind a function output binding to the return value: In a C# class library, apply the output binding attribute to the method return value. In Java, apply the output binding annotation to the function method.

Can an azure function have multiple output bindings?

Bindings are optional and a function might have one or multiple input and/or output bindings.

What is input and output binding in Azure functions?

There are two kinds of bindings you can use with functions. Input binding, which connects to a data source, our functions can read data from these inputs. Output binding which connects to a data destination, our function can write data to these destinations.

What are the types of bindings supported by Azure functions?

Azure Function Bindings Bindings are optional and come in the form of input and output bindings. An input binding is the data that your function receives. An output binding is the data that your function sends. Unlike a trigger, a function can have multiple input and output bindings.


2 Answers

I do not have the reputation yet to be able to make a comment, but in Zain Rizvi's code above, it should say IAsyncCollector:

public static async Task Run(string input, IAsyncCollector<string> collection, 
TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    await collection.AddAsync(input);
}
like image 44
Victor Pikula Avatar answered Sep 24 '22 12:09

Victor Pikula


There are a couple ways to do this:

Bind the output to the function's return value (Easiest)

Then you can simply return the value from your function. You'll have to set the output binding's name to $return in order to use this method

Code

public static async Task<string> Run(string input, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    await Task.Delay(1);

    return input;
}

Binding

{
  "bindings": [
    {
      "type": "blob",
      "name": "$return",
      "path": "testoutput/{rand-guid}.txt",
      "connection": "AzureWebJobsDashboard",
      "direction": "out"
    },
    {
      "type": "manualTrigger",
      "name": "input",
      "direction": "in"
    }
  ],
  "disabled": false
}

Bind the output to IAsyncCollector

Bind the output to IAsyncCollector and add your item to the collector.

You'll want to use this method when you have more than one output bindings.

Code

public static async Task Run(string input, IAsyncCollector<string> collection, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    await collection.AddAsync(input);
}

Binding

{
  "bindings": [
    {
      "type": "blob",
      "name": "collection",
      "path": "testoutput/{rand-guid}.txt",
      "connection": "AzureWebJobsDashboard",
      "direction": "out"
    },
    {
      "type": "manualTrigger",
      "name": "input",
      "direction": "in"
    }
  ],
  "disabled": false
}
like image 112
Zain Rizvi Avatar answered Sep 23 '22 12:09

Zain Rizvi