使用 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