Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...