Azure WebJobs Tables client library for .NET - version 1.0.0

This extension provides functionality for accessing Azure Tables in Azure Functions.

Getting started

Install the package

Install the Storage Blobs extension with NuGet:

dotnet add package Microsoft.Azure.WebJobs.Extensions.Tables --prerelease

Prerequisites

You need an Azure subscription and a Storage Account or Cosmos Tables Account to use this package.

Using Storage Tables

To create a new Storage Account, you can use the Azure Portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI:

az storage account create --name <your-resource-name> --resource-group <your-resource-group-name> --location westus --sku Standard_LRS

Using Cosmos Tables

To create a new Cosmos Tables , you can use the Azure Portal, Azure PowerShell, or the Azure CLI.

Authenticate the client

Connection represents a set of information required to connect to a table service. It can contain a connection string, an endpoint, token credential or a shared key.

The Connection property of TableAttribute defines which connection is used for the Table Service access. For example, [Tables(Connection="MyTableService")] is going to use MyTableService connection.

The connection information can be set in local.settings.json or application settings in Azure portal.

When adding a setting to local.settings.json place it under the Values property:

{
  "IsEncrypted": false,
  "Values": {
    "MyTableService": "..."
  }
}

When adding a setting to application settings in Azure portal use the provided name directly:

MyTableService = ...

Tables extension uses the AzureWebJobsStorage connection name by default.

Connection string

To use connection strings authentication assign connection string value directly to the connection setting.

<ConnectionName> = DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net

Using endpoint and token credential

NOTE: token credential authentication is supported only for storage tables.

<ConnectionName>__endpoint = https://...table.core.windows.net

If no credential information is provided the DefaultAzureCredential is used.

When using user-assigned manageed identity the clientId and credential settings need to be provided:

<ConnectionName>__credential = managedidentity

<ConnectionName>__clientId = <user-assigned client id>

Using shared key credential

When using shared key authentication the endpoint, accountKey and accountName need to be provided.

<ConnectionName>__endpoint = https://...table.core.windows.net

<ConnectionName>__credential__accountName = <account name>

<ConnectionName>__credential__accountKey = <account key>

Key concepts

The input binding allows you to read table as input to an Azure Function. The output binding allows you to modify and delete table rows in an Azure Function.

Please follow the input binding tutorial and output binding tutorial to learn about using this extension for accessing table service.

Examples

Tables extensions provides only bindings. Bindings by themselves can't trigger a function. It can only read or write entries to the table.

In the following example we use HTTP trigger to invoke the function.

Binding to a single entity

public class InputSingle
{
    [FunctionName("InputSingle")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
        [Table("MyTable", "<PartitionKey>", "<RowKey>")] TableEntity entity, ILogger log)
    {
        log.LogInformation($"PK={entity.PartitionKey}, RK={entity.RowKey}, Text={entity["Text"]}");
    }
}

Binding to a single entity using model type

public class MyEntity
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Text { get; set; }
}
public class InputSingleModel
{
    [FunctionName("InputSingleModel")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
        [Table("MyTable", "<PartitionKey>", "<RowKey>")] MyEntity entity, ILogger log)
    {
        log.LogInformation($"PK={entity.PartitionKey}, RK={entity.RowKey}, Text={entity.Text}");
    }
}

Binding to multiple entities with filter

public class InputMultipleEntitiesFilter
{
    [FunctionName("InputMultipleEntitiesFilter")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
        [Table("MyTable", "<PartitionKey>", Filter = "Text ne ''")] IEnumerable<TableEntity> entities, ILogger log)
    {
        foreach (var entity in entities)
        {
            log.LogInformation($"PK={entity.PartitionKey}, RK={entity.RowKey}, Text={entity["Text"]}");
        }
    }
}

Creating a single entity

public class OutputSingle
{
    [FunctionName("OutputSingle")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
        [Table("MyTable")] out TableEntity entity)
    {
        entity = new TableEntity("<PartitionKey>", "<RowKey>")
        {
            ["Text"] = "Hello"
        };
    }
}

Creating a single entity using model

public class MyEntity
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Text { get; set; }
}
public class OutputSingleModel
{
    [FunctionName("OutputSingleModel")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
        [Table("MyTable")] out MyEntity entity)
    {
        entity = new MyEntity()
        {
            PartitionKey = "<PartitionKey>",
            RowKey = "<RowKey>",
            Text = "Hello"
        };
    }
}

Creating multiple entities

public class OutputMultiple
{
    [FunctionName("OutputMultiple")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "POST")] HttpRequest request,
        [Table("MyTable")] IAsyncCollector<TableEntity> collector)
    {
        for (int i = 0; i < 10; i++)
        {
            collector.AddAsync(new TableEntity("<PartitionKey>", i.ToString())
            {
                ["Text"] = i.ToString()
            });
        }
    }
}

Creating multiple entities using model

public class MyEntity
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Text { get; set; }
}
public class OutputMultipleModel
{
    [FunctionName("OutputMultipleModel")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "POST")] HttpRequest request,
        [Table("MyTable")] IAsyncCollector<MyEntity> collector)
    {
        for (int i = 0; i < 10; i++)
        {
            collector.AddAsync(new MyEntity()
            {
                PartitionKey = "<PartitionKey>",
                RowKey = i.ToString(),
                Text = i.ToString()
            });
        }
    }
}

Binding to SDK TableClient type

Use a TableClient method parameter to access the table by using the Azure Tables SDK.

public class BindTableClient
{
    [FunctionName("BindTableClient")]
    public static async Task Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "POST")] HttpRequest request,
        [Table("MyTable")] TableClient client)
    {
        await client.AddEntityAsync(new TableEntity("<PartitionKey>", "<RowKey>")
        {
            ["Text"] = request.GetEncodedPathAndQuery()
        });
    }
}

Troubleshooting

Please refer to Monitor Azure Functions for troubleshooting guidance.

Next steps

Read the introduction to Azure Function or creating an Azure Function guide.

Contributing

See the CONTRIBUTING.md for details on building, testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions