適用於 Azure Functions 的 Mobile Apps 繫結Mobile Apps bindings for Azure Functions

注意

Azure Mobile Apps 繫結僅適用於 Azure Functions 1.x。Azure Mobile Apps bindings are only available to Azure Functions 1.x. Azure Functions 2.x 和更新版本中不支援它們。They are not supported in Azure Functions 2.x and higher.

本文說明如何在 Azure Functions 中使用 Azure Mobile AppsThis article explains how to work with Azure Mobile Apps bindings in Azure Functions. Azure Functions 支援 Mobile Apps 的輸入和輸出繫結。Azure Functions supports input and output bindings for Mobile Apps.

Mobile Apps 繫結可讓您讀取和更新行動裝置應用程式中的資料表。The Mobile Apps bindings let you read and update data tables in mobile apps.

這是 Azure Functions 開發人員的參考資訊。This is reference information for Azure Functions developers. 如果您不熟悉 Azure Functions,請從下列資源開始︰If you're new to Azure Functions, start with the following resources:

套件 - Functions 1.xPackages - Functions 1.x

Microsoft.Azure.WebJobs.Extensions.MobileApps NuGet 套件 1.x 版中提供 Mobile Apps 繫結。Mobile Apps bindings are provided in the Microsoft.Azure.WebJobs.Extensions.MobileApps NuGet package, version 1.x. 套件的原始程式碼位於 azure-webjobs-sdk-extensions GitHub 存放庫中。Source code for the package is in the azure-webjobs-sdk-extensions GitHub repository.

下表說明如何在每個開發環境中為此繫結新增支援。The following table tells how to add support for this binding in each development environment.

開發環境Development environment 若要新增支援To add support in
Functions 1.xFunctions 1.x
本機開發 - C# 類別庫Local development - C# class library 安裝套件Install the package
本機開發 - C# 指令碼、JavaScript、F#Local development - C# script, JavaScript, F# 自動Automatic
入口網站開發Portal development 自動Automatic

輸入Input

Mobile Apps 輸入繫結會從行動資料表端點載入記錄,並將它傳遞到您的函式。The Mobile Apps input binding loads a record from a mobile table endpoint and passes it into your function. 在 C# 和 F# 函式中,當函式成功結束時,會將記錄所做的任何變更自動傳回資料表。In C# and F# functions, any changes made to the record are automatically sent back to the table when the function exits successfully.

輸入 - 範例Input - example

請參閱特定語言的範例:See the language-specific example:

輸入 - C# 指令碼範例Input - C# script example

下列範例示範 function.json 檔案中的 Mobile Apps 輸入繫結,以及使用此繫結的 C# 指令碼函式The following example shows a Mobile Apps input binding in a function.json file and a C# script function that uses the binding. 函式會由包含記錄識別碼的佇列訊息觸發。The function is triggered by a queue message that has a record identifier. 函式會讀取指定的記錄並修改其 Text 屬性。The function reads the specified record and modifies its Text property.

以下是 function.json 檔案中的繫結資料:Here's the binding data in the function.json file:

{
"bindings": [
    {
    "name": "myQueueItem",
    "queueName": "myqueue-items",
    "connection":"",
    "type": "queueTrigger",
    "direction": "in"
    },
    {
        "name": "record",
        "type": "mobileTable",
        "tableName": "MyTable",
        "id" : "{queueTrigger}",
        "connection": "My_MobileApp_Url",
        "apiKey": "My_MobileApp_Key",
        "direction": "in"
    }
]
}

設定章節會說明這些屬性。The configuration section explains these properties.

以下是 C# 指令碼程式碼:Here's the C# script code:

#r "Newtonsoft.Json"    
using Newtonsoft.Json.Linq;

public static void Run(string myQueueItem, JObject record)
{
    if (record != null)
    {
        record["Text"] = "This has changed.";
    }    
}

輸入 - JavaScriptInput - JavaScript

下列範例示範 function.json 檔案中的 Mobile Apps 輸入繫結,以及使用此繫結的 JavaScript 指令碼函式The following example shows a Mobile Apps input binding in a function.json file and a JavaScript function that uses the binding. 函式會由包含記錄識別碼的佇列訊息觸發。The function is triggered by a queue message that has a record identifier. 函式會讀取指定的記錄並修改其 Text 屬性。The function reads the specified record and modifies its Text property.

以下是 function.json 檔案中的繫結資料:Here's the binding data in the function.json file:

{
"bindings": [
    {
    "name": "myQueueItem",
    "queueName": "myqueue-items",
    "connection":"",
    "type": "queueTrigger",
    "direction": "in"
    },
    {
        "name": "record",
        "type": "mobileTable",
        "tableName": "MyTable",
        "id" : "{queueTrigger}",
        "connection": "My_MobileApp_Url",
        "apiKey": "My_MobileApp_Key",
        "direction": "in"
    }
]
}

設定章節會說明這些屬性。The configuration section explains these properties.

以下是 JavaScript 程式碼:Here's the JavaScript code:

module.exports = function (context, myQueueItem) {    
    context.log(context.bindings.record);
    context.done();
};

輸入 - 屬性Input - attributes

C# 類別庫中,使用 MobileTable 屬性。In C# class libraries, use the MobileTable attribute.

如需您可以設定之屬性內容的相關資訊,請參閱下列組態區段For information about attribute properties that you can configure, see the following configuration section.

輸入 - 組態Input - configuration

下表說明您在 function.json 檔案中設定的繫結設定屬性內容和 MobileTable 屬性。The following table explains the binding configuration properties that you set in the function.json file and the MobileTable attribute.

function.json 屬性function.json property 屬性內容Attribute property 描述Description
typetype 必須設定為 "mobileTable"Must be set to "mobileTable"
directiondirection 必須設定為 "in"Must be set to "in"
namename 函式簽章中的輸入參數名稱。Name of input parameter in function signature.
tableNametableName TableNameTableName 行動裝置應用程式資料表的名稱Name of the mobile app's data table
idid IdId 要擷取之記錄的識別碼。The identifier of the record to retrieve. 可以是靜態,或以叫用函式的觸發程序作為基礎。Can be static or based on the trigger that invokes the function. 例如,如果您對函式使用佇列觸發程序,"id": "{queueTrigger}" 就會使用佇列訊息的字串值作為要擷取的記錄識別碼。For example, if you use a queue trigger for your function, then "id": "{queueTrigger}" uses the string value of the queue message as the record ID to retrieve.
連接connection 連接Connection 包含行動裝置應用程式 URL 的應用程式設定名稱。The name of an app setting that has the mobile app's URL. 函式會使用此 URL 針對您的行動裝置應用程式建構所需的 REST 作業。The function uses this URL to construct the required REST operations against your mobile app. 在包含您的行動裝置應用程式 URL 的函式應用程式中建立應用程式設定,然後在輸入繫結的 connection 屬性中,指定應用程式設定的名稱。Create an app setting in your function app that contains the mobile app's URL, then specify the name of the app setting in the connection property in your input binding. URL 看起來像這樣:http://<appname>.azurewebsites.netThe URL looks like http://<appname>.azurewebsites.net.
apiKeyapiKey ApiKeyApiKey 包含行動裝置應用程式 API 金鑰的應用程式設定名稱。The name of an app setting that has your mobile app's API key. 如果您在您的 Node.js 行動裝置應用程式中實作 API 金鑰,或在您的 .NET 行動裝置應用程式中實作 API 金鑰,請提供 API 金鑰。Provide the API key if you implement an API key in your Node.js mobile app, or implement an API key in your .NET mobile app. 請提供金鑰,在包含 API 金鑰的函式應用程式中建立應用程式設定,然後在具有應用程式設定名稱的輸入繫結中新增 apiKey 屬性。To provide the key, create an app setting in your function app that contains the API key, then add the apiKey property in your input binding with the name of the app setting.

當您要在本機開發時,應用程式設定會進入 local.settings.json 檔案When you're developing locally, app settings go into the local.settings.json file.

重要

請勿與行動裝置應用程式用戶端共用 API 金鑰。Don't share the API key with your mobile app clients. 只應該安全地散佈給服務端用戶端,如 Azure Functions。It should only be distributed securely to service-side clients, like Azure Functions. Azure Functions 將會您的連接資訊和 API 金鑰儲存為應用程式設定,使得不會將讓它們簽入至您的原始檔控制儲存機制。Azure Functions stores your connection information and API keys as app settings so that they are not checked into your source control repository. 這可保護您的敏感資訊。This safeguards your sensitive information.

輸入 - 使用方式Input - usage

在 C# 函式中,找到具有指定識別碼的記錄時,它會傳遞到名為 JObject 的參數。In C# functions, when the record with the specified ID is found, it is passed into the named JObject parameter. 找不到記錄時,參數值為 nullWhen the record is not found, the parameter value is null.

在 JavaScript 函式中,記錄會傳遞至 context.bindings.<name> 物件。In JavaScript functions, the record is passed into the context.bindings.<name> object. 找不到記錄時,參數值為 nullWhen the record is not found, the parameter value is null.

在 C# 和 F# 函式中,當函式成功結束時,會將對輸入記錄 (輸入參數) 所做的任何變更自動傳送回資料表。In C# and F# functions, any changes you make to the input record (input parameter) are automatically sent back to the table when the function exits successfully. 您無法修改 JavaScript 函式中的記錄。You can't modify a record in JavaScript functions.

輸出Output

使用 Mobile Apps 輸出繫結將新記錄寫入至 Mobile Apps 資料表。Use the Mobile Apps output binding to write a new record to a Mobile Apps table.

輸出 - 範例Output - example

請參閱特定語言的範例:See the language-specific example:

輸出 - C# 範例Output - C# example

下列範例示範的 C# 函式是由佇列訊息觸發,並在行動應用程式資料表中建立記錄。The following example shows a C# function that is triggered by a queue message and creates a record in a mobile app table.

[FunctionName("MobileAppsOutput")]        
[return: MobileTable(ApiKeySetting = "MyMobileAppKey", TableName = "MyTable", MobileAppUriSetting = "MyMobileAppUri")]
public static object Run(
    [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
    TraceWriter log)
{
    return new { Text = $"I'm running in a C# function! {myQueueItem}" };
}

輸出 - C# 指令碼範例Output - C# script example

下列範例示範 function.json 檔案中的 Mobile Apps 輸出繫結,以及使用此繫結的 C# 指令碼函式The following example shows a Mobile Apps output binding in a function.json file and a C# script function that uses the binding. 函式會由佇列訊息觸發,並使用 Text 屬性的硬式編碼值來建立新的記錄。The function is triggered by a queue message and creates a new record with hard-coded value for the Text property.

以下是 function.json 檔案中的繫結資料:Here's the binding data in the function.json file:

{
"bindings": [
    {
    "name": "myQueueItem",
    "queueName": "myqueue-items",
    "connection":"",
    "type": "queueTrigger",
    "direction": "in"
    },
    {
    "name": "record",
    "type": "mobileTable",
    "tableName": "MyTable",
    "connection": "My_MobileApp_Url",
    "apiKey": "My_MobileApp_Key",
    "direction": "out"
    }
]
}

設定章節會說明這些屬性。The configuration section explains these properties.

以下是 C# 指令碼程式碼:Here's the C# script code:

public static void Run(string myQueueItem, out object record)
{
    record = new {
        Text = $"I'm running in a C# function! {myQueueItem}"
    };
}

輸出 - JavaScript 範例Output - JavaScript example

下列範例示範 function.json 檔案中的 Mobile Apps 輸出繫結,以及使用此繫結的 JavaScript 指令碼函式The following example shows a Mobile Apps output binding in a function.json file and a JavaScript function that uses the binding. 函式會由佇列訊息觸發,並使用 Text 屬性的硬式編碼值來建立新的記錄。The function is triggered by a queue message and creates a new record with hard-coded value for the Text property.

以下是 function.json 檔案中的繫結資料:Here's the binding data in the function.json file:

{
"bindings": [
    {
    "name": "myQueueItem",
    "queueName": "myqueue-items",
    "connection":"",
    "type": "queueTrigger",
    "direction": "in"
    },
    {
    "name": "record",
    "type": "mobileTable",
    "tableName": "MyTable",
    "connection": "My_MobileApp_Url",
    "apiKey": "My_MobileApp_Key",
    "direction": "out"
    }
],
"disabled": false
}

設定章節會說明這些屬性。The configuration section explains these properties.

以下是 JavaScript 程式碼:Here's the JavaScript code:

module.exports = function (context, myQueueItem) {

    context.bindings.record = {
        text : "I'm running in a Node function! Data: '" + myQueueItem + "'"
    }   

    context.done();
};

輸出 - 屬性Output - attributes

C# 類別庫中,使用 MobileTable 屬性。In C# class libraries, use the MobileTable attribute.

如需可設定的屬性內容相關資訊,請參閱輸出 - 組態For information about attribute properties that you can configure, see Output - configuration. 以下是方法簽章中的 MobileTable 屬性範例:Here's a MobileTable attribute example in a method signature:

[FunctionName("MobileAppsOutput")]        
[return: MobileTable(ApiKeySetting = "MyMobileAppKey", TableName = "MyTable", MobileAppUriSetting = "MyMobileAppUri")]
public static object Run(
    [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
    TraceWriter log)
{
    ...
}

如需完整範例,請參閱輸出 - C# 範例For a complete example, see Output - C# example.

輸出 - 設定Output - configuration

下表說明您在 function.json 檔案中設定的繫結設定屬性內容和 MobileTable 屬性。The following table explains the binding configuration properties that you set in the function.json file and the MobileTable attribute.

function.json 屬性function.json property 屬性內容Attribute property 描述Description
typetype 必須設定為 "mobileTable"Must be set to "mobileTable"
directiondirection 必須設定為 "out"Must be set to "out"
namename 函式簽章中的輸出參數名稱。Name of output parameter in function signature.
tableNametableName TableNameTableName 行動裝置應用程式資料表的名稱Name of the mobile app's data table
連接connection MobileAppUriSettingMobileAppUriSetting 包含行動裝置應用程式 URL 的應用程式設定名稱。The name of an app setting that has the mobile app's URL. 函式會使用此 URL 針對您的行動裝置應用程式建構所需的 REST 作業。The function uses this URL to construct the required REST operations against your mobile app. 在包含您的行動裝置應用程式 URL 的函式應用程式中建立應用程式設定,然後在輸入繫結的 connection 屬性中,指定應用程式設定的名稱。Create an app setting in your function app that contains the mobile app's URL, then specify the name of the app setting in the connection property in your input binding. URL 看起來像這樣:http://<appname>.azurewebsites.netThe URL looks like http://<appname>.azurewebsites.net.
apiKeyapiKey ApiKeySettingApiKeySetting 包含行動裝置應用程式 API 金鑰的應用程式設定名稱。The name of an app setting that has your mobile app's API key. 如果您在您的 Node.js 行動裝置應用程式後端中實作 API 金鑰,或在您的 .NET 行動裝置應用程式後端中實作 API 金鑰,請提供 API 金鑰。Provide the API key if you implement an API key in your Node.js mobile app backend, or implement an API key in your .NET mobile app backend. 請提供金鑰,在包含 API 金鑰的函式應用程式中建立應用程式設定,然後在具有應用程式設定名稱的輸入繫結中新增 apiKey 屬性。To provide the key, create an app setting in your function app that contains the API key, then add the apiKey property in your input binding with the name of the app setting.

當您要在本機開發時,應用程式設定會進入 local.settings.json 檔案When you're developing locally, app settings go into the local.settings.json file.

重要

請勿與行動裝置應用程式用戶端共用 API 金鑰。Don't share the API key with your mobile app clients. 只應該安全地散佈給服務端用戶端,如 Azure Functions。It should only be distributed securely to service-side clients, like Azure Functions. Azure Functions 將會您的連接資訊和 API 金鑰儲存為應用程式設定,使得不會將讓它們簽入至您的原始檔控制儲存機制。Azure Functions stores your connection information and API keys as app settings so that they are not checked into your source control repository. 這可保護您的敏感資訊。This safeguards your sensitive information.

輸出 - 使用方式Output - usage

在 C# 指令碼函式中,使用類型 out object 的具名輸出參數來存取輸出記錄。In C# script functions, use a named output parameter of type out object to access the output record. 在 C# 類別庫中,MobileTable 屬性可與下列任何類型搭配使用:In C# class libraries, the MobileTable attribute can be used with any of the following types:

  • ICollector<T>IAsyncCollector<T>,其中 TJObject 或任何包含 public string Id 屬性的類型。ICollector<T> or IAsyncCollector<T>, where T is either JObject or any type with a public string Id property.
  • out JObject
  • out Tout T[],其中 T 為任何包含 public string Id 屬性的類型。out T or out T[], where T is any Type with a public string Id property.

在 Node.js 函式中,使用 context.bindings.<name> 來存取輸出記錄。In Node.js functions, use context.bindings.<name> to access the output record.

後續步驟Next steps