इसे छोड़कर कंटेंट पर जाएं
Docs Try Aspire
Docs Try

Connect to Azure Table Storage

यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।

Azure Table Storage logo

This page describes how consuming apps connect to an Azure Table Storage resource that’s already modeled in your AppHost. For the AppHost API surface — adding a storage account, table resources, Azurite emulator configuration, and more — see Azure Table Storage Hosting integration.

When you reference an Azure Table Storage resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire Azure Data Tables client integration for automatic dependency injection, health checks, and telemetry.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the ConnectionString property of a resource called tables becomes TABLES_CONNECTIONSTRING.

The Azure Table Storage resource exposes the following connection properties:

Property NameEnvironment variableDescription
ConnectionString{RESOURCE}_CONNECTIONSTRINGThe full Azure Storage connection string. Available when running locally with the Azurite emulator, or when configured for key-based access in production.
TableEndpoint{RESOURCE}_TABLEENDPOINTThe URI of the Table Storage service endpoint, with the format https://{accountname}.table.core.windows.net/. Available when using Azure Managed Identity or token-based access in production.

Example values:

TABLES_CONNECTIONSTRING: DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=...;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;
TABLES_TABLEENDPOINT: https://mystorageaccount.table.core.windows.net/

Pick the language your consuming app is written in. Each example assumes your AppHost adds an Azure Table Storage resource named tables and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Azure Data Tables client integration. It registers a TableServiceClient through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.

Install the 📦 Aspire.Azure.Data.Tables NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.Data.Tables package
dotnet add package Aspire.Azure.Data.Tables

In Program.cs, call AddAzureTableServiceClient on your IHostApplicationBuilder to register a TableServiceClient:

C# — Program.cs
builder.AddAzureTableServiceClient(connectionName: "tables");

Resolve the service client through dependency injection:

C# — ExampleService.cs
public class ExampleService(TableServiceClient client)
{
// Use client...
}

To register multiple TableServiceClient instances with different connection names, use AddKeyedAzureTableServiceClient:

C# — Program.cs
builder.AddKeyedAzureTableServiceClient(name: "tables1");
builder.AddKeyedAzureTableServiceClient(name: "tables2");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("tables1")] TableServiceClient tablesClient1,
[FromKeyedServices("tables2")] TableServiceClient tablesClient2)
{
// Use clients...
}

For more information, see .NET dependency injection: Keyed services.

The Aspire Azure Data Tables client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddAzureTableServiceClient:

C# — Program.cs
builder.AddAzureTableServiceClient("tables");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"tables": "DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=...;EndpointSuffix=core.windows.net"
}
}

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads AzureDataTablesSettings from appsettings.json (or any other configuration source) using the Aspire:Azure:Data:Tables key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Data": {
"Tables": {
"ServiceUri": "https://myaccount.table.core.windows.net/",
"DisableHealthChecks": false,
"DisableTracing": false,
"ClientOptions": {
"EnableTenantDiscovery": true
}
}
}
}
}
}

Inline delegates. Pass an Action<AzureDataTablesSettings> to configure settings inline:

C# — Program.cs
builder.AddAzureTableServiceClient(
"tables",
settings => settings.DisableHealthChecks = true);

You can also configure the TableClientOptions:

C# — Program.cs
builder.AddAzureTableServiceClient(
"tables",
configureClientBuilder: clientBuilder =>
clientBuilder.ConfigureOptions(
options => options.EnableTenantDiscovery = true));

Aspire client integrations enable health checks by default. The Azure Data Tables client integration adds:

  • A health check that attempts to connect to the Azure Table Storage service.
  • Integration with the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

To disable health checks, set DisableHealthChecks to true in the configuration.

The Aspire Azure Data Tables client integration automatically configures logging and tracing through OpenTelemetry.

Logging categories:

  • Azure.Core
  • Azure.Identity

Tracing activities:

  • Azure.Data.Tables.TableServiceClient

Metrics: The Azure Data Tables integration currently doesn’t support metrics by default due to limitations in the Azure SDK.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection string from the environment and construct a TableServiceClient directly using the 📦 Azure.Data.Tables NuGet package:

C# — Program.cs
using Azure.Data.Tables;
var connectionString = Environment.GetEnvironmentVariable("TABLES_CONNECTIONSTRING");
var tableEndpoint = Environment.GetEnvironmentVariable("TABLES_TABLEENDPOINT");
// Use connection string (emulator or key-based access)
var client = !string.IsNullOrEmpty(connectionString)
? new TableServiceClient(connectionString)
: new TableServiceClient(new Uri(tableEndpoint!));
// Get a client for a specific table
var tableClient = client.GetTableClient("mytable");
await tableClient.CreateIfNotExistsAsync();