您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 Azure 函数返回值Using the Azure Function return value

本文解释了返回值在函数内如何工作。This article explains how return values work inside a function.

在支持返回值的语言中,可以将函数输出绑定绑定到返回值:In languages that have a return value, you can bind a function output binding to the return value:

  • 在 C# 类库,请将输出绑定特性应用到方法返回值。In a C# class library, apply the output binding attribute to the method return value.
  • 在其他语言中,请将 function.json 中的 name 属性设置为 $returnIn other languages, set the name property in function.json to $return.

如果有多个输出绑定,请只使用其中一个绑定的返回值。If there are multiple output bindings, use the return value for only one of them.

在 C# 和 C# 脚本中,将数据发送到输出绑定的替代方法是使用 out 参数和收集器对象In C# and C# script, alternative ways to send data to an output binding are out parameters and collector objects.

请参阅演示如何使用返回值的特定于语言的示例:See the language-specific example showing use of the return value:

C# 示例C# example

以下 C# 代码使用输出绑定的返回值,后接异步示例:Here's C# code that uses the return value for an output binding, followed by an async example:

[FunctionName("QueueTrigger")]
[return: Blob("output-container/{id}")]
public static string Run([QueueTrigger("inputqueue")]WorkItem input, ILogger log)
{
    string json = string.Format("{{ \"id\": \"{0}\" }}", input.Id);
    log.LogInformation($"C# script processed queue message. Item={json}");
    return json;
}
[FunctionName("QueueTrigger")]
[return: Blob("output-container/{id}")]
public static Task<string> Run([QueueTrigger("inputqueue")]WorkItem input, ILogger log)
{
    string json = string.Format("{{ \"id\": \"{0}\" }}", input.Id);
    log.LogInformation($"C# script processed queue message. Item={json}");
    return Task.FromResult(json);
}

C# 脚本示例C# script example

下面是 function.json 文件中的输出绑定:Here's the output binding in the function.json file:

{
    "name": "$return",
    "type": "blob",
    "direction": "out",
    "path": "output-container/{id}"
}

下面是 C# 脚本代码,后接异步示例:Here's the C# script code, followed by an async example:

public static string Run(WorkItem input, ILogger log)
{
    string json = string.Format("{{ \"id\": \"{0}\" }}", input.Id);
    log.LogInformation($"C# script processed queue message. Item={json}");
    return json;
}
public static Task<string> Run(WorkItem input, ILogger log)
{
    string json = string.Format("{{ \"id\": \"{0}\" }}", input.Id);
    log.LogInformation($"C# script processed queue message. Item={json}");
    return Task.FromResult(json);
}

F# 示例F# example

下面是 function.json 文件中的输出绑定:Here's the output binding in the function.json file:

{
    "name": "$return",
    "type": "blob",
    "direction": "out",
    "path": "output-container/{id}"
}

F# 代码如下所示:Here's the F# code:

let Run(input: WorkItem, log: ILogger) =
    let json = String.Format("{{ \"id\": \"{0}\" }}", input.Id)   
    log.LogInformation(sprintf "F# script processed queue message '%s'" json)
    json

JavaScript 示例JavaScript example

下面是 function.json 文件中的输出绑定:Here's the output binding in the function.json file:

{
    "name": "$return",
    "type": "blob",
    "direction": "out",
    "path": "output-container/{id}"
}

在 JavaScript 中,返回值位于 context.done 的第二个参数中:In JavaScript, the return value goes in the second parameter for context.done:

module.exports = function (context, input) {
    var json = JSON.stringify(input);
    context.log('Node.js script processed queue message', json);
    context.done(null, json);
}

Python 示例Python example

下面是 function.json 文件中的输出绑定:Here's the output binding in the function.json file:

{
    "name": "$return",
    "type": "blob",
    "direction": "out",
    "path": "output-container/{id}"
}

下面是 Python 代码:Here's the Python code:

def main(input: azure.functions.InputStream) -> str:
    return json.dumps({
        'name': input.name,
        'length': input.length,
        'content': input.read().decode('utf-8')
    })

后续步骤Next steps