简体   繁体   English

如何将输出值绑定到异步Azure功能?

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

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. 将参数设置为out的常用方法不适用于异步函数。

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) 使用绑定(fyi)

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

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 您必须将输出绑定的名称设置为$return才能使用此方法

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 将输出绑定到IAsyncCollector

Bind the output to IAsyncCollector and add your item to the collector. 将输出绑定到IAsyncCollector并将项目添加到收集器。

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
}

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: 我还没有声誉可以发表评论,但在上面的Zain Rizvi的代码中,应该说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);
}

Async methods can return values normally, but you shouldn't return the pure type of value, use Task instead, like this: 异步方法可以正常返回值,但是您不应该返回纯类型的值,而是使用Task,如下所示:

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

        blobOutput = input;
        return blobOutput;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM