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


如何将输出绑定到异步函数?将参数设置为的常用方法 out 不适用于异步功能。

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;
}

这导致编译错误:

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

使用绑定(fyi)

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

10401
2017-11-03 19:03


起源



答案:


有几种方法可以做到这一点:

将输出绑定到函数的返回值(最简单)

然后,您只需返回函数中的值即可。您必须将输出绑定的名称设置为 $return 为了使用这种方法

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;
}

捆绑

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

将输出绑定到IAsyncCollector

将输出绑定到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);
}

捆绑

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

13
2017-11-03 19:03



我使用时遇到问题: ICollectorAsync : error CS0246: The type or namespace name 'ICollectorAsync<>' could not be found - Jean-philippe Emond
@ Jean-philippeEmond对不起,应该是IAsyncCollector - Zain Rizvi
Blob输出绑定不支持收集器,请参阅此内容 问题。看到 这个答案 另一种可能性。 - Mikhail


答案:


有几种方法可以做到这一点:

将输出绑定到函数的返回值(最简单)

然后,您只需返回函数中的值即可。您必须将输出绑定的名称设置为 $return 为了使用这种方法

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;
}

捆绑

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

将输出绑定到IAsyncCollector

将输出绑定到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);
}

捆绑

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

13
2017-11-03 19:03



我使用时遇到问题: ICollectorAsync : error CS0246: The type or namespace name 'ICollectorAsync<>' could not be found - Jean-philippe Emond
@ Jean-philippeEmond对不起,应该是IAsyncCollector - Zain Rizvi
Blob输出绑定不支持收集器,请参阅此内容 问题。看到 这个答案 另一种可能性。 - Mikhail


我还没有声誉可以发表评论,但在上面的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);
}

2
2018-05-29 21:57



谢谢,修好了 - Zain Rizvi


异步方法可以正常返回值,但是您不应该返回纯类型的值,而是使用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;
    }

0
2017-11-03 19:18



卫生署!是的,我原本打算将返回值设置为Task <string>。我原来的答案概率的功能甚至不起作用。谢谢,我现在修好了 - Zain Rizvi
不要忘记,这还要求您将function.json中的“name”属性设置为“$ return”以进行输出绑定。这告诉运行时使用函数的输出作为源。 - Tim P.