Skip to content

Latest commit

 

History

History
319 lines (240 loc) · 13.2 KB

functions-bindings-mobile-apps.md

File metadata and controls

319 lines (240 loc) · 13.2 KB
title description author ms.topic ms.date ms.author
Mobile Apps bindings for Azure Functions
Understand how to use Azure Mobile Apps bindings in Azure Functions.
craigshoemaker
reference
11/21/2017
cshoe

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.

[!INCLUDE intro]

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.

[!INCLUDE functions-package]

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:

Input - C# script 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 - 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. 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 JavaScript code:

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

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 http://<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.

[!INCLUDE app settings to local.settings.json]

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

See the language-specific example:

Output - C# 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 - C# script example

The following example shows a Mobile Apps output binding in a function.json file and a C# script function that uses the binding. The function is triggered by a queue message and creates a new record with hard-coded value for the 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",
        "connection": "My_MobileApp_Url",
        "apiKey": "My_MobileApp_Key",
        "direction": "out"
    }
]
}

The configuration section explains these properties.

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

Output - JavaScript example

The following example shows a Mobile Apps output binding in a function.json file and a JavaScript function that uses the binding. The function is triggered by a queue message and creates a new record with hard-coded value for the 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",
        "connection": "My_MobileApp_Url",
        "apiKey": "My_MobileApp_Key",
        "direction": "out"
    }
],
"disabled": false
}

The configuration section explains these properties.

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

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)
{
    ...
}

For a complete example, see Output - C# example.

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 http://<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.

[!INCLUDE app settings to local.settings.json]

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

[!div class="nextstepaction"] Learn more about Azure functions triggers and bindings