使用 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.
在 Java 中,将输出绑定注释应用于函数方法。 In Java, apply the output binding annotation to the function method.
在其他语言中,请将 function.json 中的 name
属性设置为 $return
。 In 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 .
以下 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);
}
下面是 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);
}
下面是 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
下面是 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);
}
下面是 function.json 文件中的输出绑定: Here's the output binding in the function.json file:
{
"name": "Response",
"type": "blob",
"direction": "out",
"path": "output-container/{blobname}"
}
以下 PowerShell 代码使用 http 输出绑定的返回值: Here's the PowerShell code that uses the return value for an http output binding:
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $blobname
})
以下 Java 代码使用输出绑定的返回值: Here's Java code that uses the return value for an output binding:
@FunctionName("QueueTrigger")
@StorageAccount("AzureWebJobsStorage")
@BlobOutput(name = "output", path = "output-container/{id}")
public static String run(
@QueueTrigger(name = "input", queueName = "inputqueue") WorkItem input,
final ExecutionContext context
) {
String json = String.format("{ \"id\": \"%s\" }", input.id);
context.getLogger().info("Java processed queue message. Item=" + json);
return json;
}
后续步骤 Next steps