.NET Aspire Azure Data Tables component

In this article, you learn how to use the .NET Aspire Azure Data Tables component. The Aspire.Azure.Data.Tables library is used to:

  • Registers a TableServiceClient as a singleton in the DI container for connecting to Azure Table storage.
  • Enables corresponding health checks, logging and telemetry.

Prerequisites

Get started

To get started with the .NET Aspire Azure Data Tables component, install the Aspire.Azure.Data.Tables NuGet package.

dotnet add package Aspire.Azure.Data.Tables

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

In the Program.cs file of your component-consuming project, call the AddAzureTableClient extension to register a TableServiceClient for use via the dependency injection container.

builder.AddAzureTableClient("tables");

To retrieve the TableServiceClient instance using dependency injection, define it as a constructor parameter. Consider the following example service:

public class ExampleService(TableServiceClient client)
{
    // Use client...
}

App host usage

To add Azure Storage hosting support to your IDistributedApplicationBuilder, install the Aspire.Hosting.Azure.Storage NuGet package.

dotnet add package Aspire.Hosting.Azure.Storage

In your app host project, register the Azure Table Storage component and consume the service using the following methods:

var builder = DistributedApplication.CreateBuilder(args);

var tables = builder.AddAzureStorage("storage")
                    .AddTables("tables");

Builder.AddProject<MyApp.ExampleProject>() 
       .WithReference(tables)

For more information, see WithReference.

Configuration

The .NET Aspire Azure Table Storage component provides multiple options to configure the TableServiceClient based on the requirements and conventions of your project.

Use configuration providers

The .NET Aspire Azure Table Storage component supports Microsoft.Extensions.Configuration. It loads the AzureDataTablesSettings from appsettings.json or other configuration files using Aspire:Azure:Data:Tables key.

{
  "Aspire":{
    "Azure": {
      "Data": {
        "Tables": {
          "ServiceUri": "YOUR_URI",
          "DisableHealthChecks": true,
          "DisableTracing": false,
          "ClientOptions": {
          "EnableTenantDiscovery": true
          }
        }
      }
    }
  }
}

If you have set up your configurations in the Aspire:Azure:Data:Tables section of your appsettings.json file you can just call the method AddAzureTableClient without passing any parameters.

Use inline delegates

You can also pass the Action<AzureDataTablesSettings> delegate to set up some or all the options inline, for example to set the ServiceUri:

builder.AddAzureTableClient(
    "tables",
    static settings => settings.ServiceUri = new Uri("YOUR_SERVICEURI"));

You can also set up the TableClientOptions using Action<IAzureClientBuilder<TableServiceClient, TableClientOptions>> delegate, the second parameter of the AddAzureTableClient method. For example to set the TableServiceClient ID to identify the client:

builder.AddAzureTableClient(
    "tables",
    static clientBuilder =>
        clientBuilder.ConfigureOptions(
            static options => options.EnableTenantDiscovery = true));

Named instances

If you want to add more than one TableServiceClient you can use named instances. Load the named configuration section from the json config by calling the AddAzureTableClient method and passing in the INSTANCE_NAME.

builder.AddAzureTableClient("INSTANCE_NAME");

The corresponding configuration JSON is defined as follows:

{
  "Aspire":{
    "Azure": {
      "Data": {
        "Tables": {
          "INSTANCE_NAME": {
            "ServiceUri": "YOUR_URI",
            "DisableHealthChecks": true,
            "ClientOptions": {
              "EnableTenantDiscovery": true
            }
          }
        }
      }
    }
  }
}

Configuration options

The following configurable options are exposed through the AzureDataTablesSettings class:

Name Description
ServiceUri A "Uri" referencing the Table service.
Credential The credential used to authenticate to the Table Storage.
DisableHealthChecks A boolean value that indicates whether the Table Storage health check is disabled or not.
DisableTracing A boolean value that indicates whether the OpenTelemetry tracing is disabled or not.

Health checks

By default, .NET Aspire components enable health checks for all services. For more information, see .NET Aspire components overview.

By default, The .NET Aspire Azure Data Tables component handles the following:

  • Adds the AzureTableStorageHealthCheck health check, which attempts to connect to and query table storage
  • Integrates with the /health HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic

Observability and telemetry

.NET Aspire components automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about component observability and telemetry, see .NET Aspire components overview. Depending on the backing service, some components may only support some of these features. For example, some components support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The .NET Aspire Azure Data Tables component uses the following log categories:

  • Azure.Core
  • Azure.Identity

Tracing

The .NET Aspire Azure Data Tables component will emit the following tracing activities using OpenTelemetry:

  • "Azure.Data.Tables.TableServiceClient"

Metrics

The .NET Aspire Azure Data Tables component currently does not support metrics by default due to limitations with the Azure SDK.

See also