如何将输出值绑定到异步Azure函数? [英] How can I bind output values to my async Azure Function?

查看:54
本文介绍了如何将输出值绑定到异步Azure函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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
}

推荐答案

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

There are a couple ways to do this:

然后,您可以简单地从函数中返回值.您必须将输出绑定的名称设置为$return才能使用此方法

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

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并将您的项目添加到收集器.

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.

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
}

这篇关于如何将输出值绑定到异步Azure函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆