Azure Functions 的移动应用绑定Mobile Apps bindings for Azure Functions

Note

Azure 移动应用绑定仅适用于 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.

本文介绍如何在 Azure Functions 中使用 Azure 移动应用绑定。This article explains how to work with Azure Mobile Apps bindings in Azure Functions. Azure Functions 支持移动应用的输入和输出绑定。Azure Functions supports input and output bindings for 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 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

移动应用输入绑定从移动表终结点加载记录,并将其传递到函数中。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 文件中的一个移动应用输入绑定以及使用该绑定的 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 文件中的一个移动应用输入绑定以及使用该绑定的 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 属性Attribute property 说明Description
类型type 必须设置为“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}" 会使用队列消息的字符串值作为记录 ID 进行检索。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 ConnectionConnection 包含移动应用 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>.chinacloudsites.cnThe URL looks like http://<appname>.chinacloudsites.cn.
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.

Important

不要与移动应用客户端共享该 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# 函数中,当找到具有指定 ID 的记录时,该记录会传递到命名的 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

使用移动应用输出绑定将新记录写入移动应用表。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 文件中的一个移动应用输出绑定以及使用该绑定的 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 文件中的一个移动应用输出绑定以及使用该绑定的 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 属性Attribute property 说明Description
类型type 必须设置为“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>.chinacloudsites.cnThe URL looks like http://<appname>.chinacloudsites.cn.
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.

Important

不要与移动应用客户端共享该 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