Επεξεργασία

Κοινή χρήση μέσω


Mobile Apps bindings for Azure Functions

Note

Azure Mobile Apps bindings are only available to Azure Functions 1.x. They are not supported in Azure Functions 2.x and higher.

This article explains how to work with Azure Mobile Apps bindings in 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.

Packages - Functions 1.x

Mobile Apps bindings are provided in the Microsoft.Azure.WebJobs.Extensions.MobileApps NuGet package, version 1.x. Source code for the package is in the azure-webjobs-sdk-extensions GitHub repository.

The following table lists how to add support for output binding in each development environment.

Development environment To add support in Functions 1.x
Local development: C# class library Install the package
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. 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:

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. The function reads the specified record and modifies its Text property.

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.

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.";
    }    
}

Input - attributes

In C# class libraries, use the MobileTable attribute.

For information about attribute properties that you can configure, see the following configuration section.

Input - configuration

The following table explains the binding configuration properties that you set in the function.json file and the MobileTable attribute.

function.json property Attribute property Description
type n/a Must be set to "mobileTable"
direction n/a Must be set to "in"
name n/a Name of input parameter in function signature.
tableName TableName Name of the mobile app's data table
id Id The identifier of the record to retrieve. Can be static or based on the trigger that invokes the function. 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 The name of an app setting that has the mobile app's URL. The function uses this URL to construct the required REST operations against your mobile app. 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. The URL looks like https://<appname>.azurewebsites.net.
apiKey ApiKey The name of an app setting that has your mobile app's API key. 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. 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.

When you're developing locally, add your application settings in the local.settings.json file in the Values collection.

Important

Don't share the API key with your mobile app clients. It should only be distributed securely to service-side clients, like Azure Functions. 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

In C# functions, when the record with the specified ID is found, it is passed into the named JObject parameter. When the record is not found, the parameter value is null.

In JavaScript functions, the record is passed into the context.bindings.<name> object. When the record is not found, the parameter value is null.

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. 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

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}" };
}

Output - attributes

In C# class libraries, use the MobileTable attribute.

For information about attribute properties that you can configure, see Output - configuration. 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)
{
    ...
}

Output - configuration

The following table explains the binding configuration properties that you set in the function.json file and the MobileTable attribute.

function.json property Attribute property Description
type n/a Must be set to "mobileTable"
direction n/a Must be set to "out"
name n/a Name of output parameter in function signature.
tableName TableName Name of the mobile app's data table
connection MobileAppUriSetting The name of an app setting that has the mobile app's URL. The function uses this URL to construct the required REST operations against your mobile app. 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. The URL looks like https://<appname>.azurewebsites.net.
apiKey ApiKeySetting The name of an app setting that has your mobile app's API key. 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. 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.

When you're developing locally, add your application settings in the local.settings.json file in the Values collection.

Important

Don't share the API key with your mobile app clients. It should only be distributed securely to service-side clients, like Azure Functions. 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

In C# script functions, use a named output parameter of type out object to access the output record. In C# class libraries, the MobileTable attribute can be used with any of the following types:

  • ICollector<T> or IAsyncCollector<T>, where T is either JObject or any type with a public string Id property.
  • out JObject
  • out T or out T[], where T is any Type with a public string Id property.

In Node.js functions, use context.bindings.<name> to access the output record.

Next steps