Connect to Azure Table Storage
यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।
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.
Connection properties
Section titled “Connection properties”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 Name | Environment variable | Description |
|---|---|---|
ConnectionString | {RESOURCE}_CONNECTIONSTRING | The full Azure Storage connection string. Available when running locally with the Azurite emulator, or when configured for key-based access in production. |
TableEndpoint | {RESOURCE}_TABLEENDPOINT | The 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/Connect from your app
Section titled “Connect from your app”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 client integration
Section titled “Install the client integration”Install the 📦 Aspire.Azure.Data.Tables NuGet package in the client-consuming project:
dotnet add package Aspire.Azure.Data.Tables#:package Aspire.Azure.Data.Tables@*<PackageReference Include="Aspire.Azure.Data.Tables" Version="*" />Add the Table Storage client
Section titled “Add the Table Storage client”In Program.cs, call AddAzureTableServiceClient on your IHostApplicationBuilder to register a TableServiceClient:
builder.AddAzureTableServiceClient(connectionName: "tables");Resolve the service client through dependency injection:
public class ExampleService(TableServiceClient client){ // Use client...}Add keyed Table Storage clients
Section titled “Add keyed Table Storage clients”To register multiple TableServiceClient instances with different connection names, use AddKeyedAzureTableServiceClient:
builder.AddKeyedAzureTableServiceClient(name: "tables1");builder.AddKeyedAzureTableServiceClient(name: "tables2");Then resolve each instance by key:
public class ExampleService( [FromKeyedServices("tables1")] TableServiceClient tablesClient1, [FromKeyedServices("tables2")] TableServiceClient tablesClient2){ // Use clients...}For more information, see .NET dependency injection: Keyed services.
Configuration
Section titled “Configuration”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:
builder.AddAzureTableServiceClient("tables");The connection string is resolved from the ConnectionStrings section:
{ "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:
{ "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:
builder.AddAzureTableServiceClient( "tables", settings => settings.DisableHealthChecks = true);You can also configure the TableClientOptions:
builder.AddAzureTableServiceClient( "tables", configureClientBuilder: clientBuilder => clientBuilder.ConfigureOptions( options => options.EnableTenantDiscovery = true));Client integration health checks
Section titled “Client integration health checks”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
/healthHTTP 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.
Observability and telemetry
Section titled “Observability and telemetry”The Aspire Azure Data Tables client integration automatically configures logging and tracing through OpenTelemetry.
Logging categories:
Azure.CoreAzure.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.
Read environment variables in C#
Section titled “Read environment variables in C#”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:
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 tablevar tableClient = client.GetTableClient("mytable");await tableClient.CreateIfNotExistsAsync();Use the azure-sdk-for-go/sdk/data/aztables package:
go get github.com/Azure/azure-sdk-for-go/sdk/data/aztablesRead the injected environment variables and connect:
package main
import ( "context" "os"
"github.com/Azure/azure-sdk-for-go/sdk/data/aztables")
func main() { connectionString := os.Getenv("TABLES_CONNECTIONSTRING") tableEndpoint := os.Getenv("TABLES_TABLEENDPOINT")
var client *aztables.ServiceClient var err error
if connectionString != "" { // Emulator or key-based access client, err = aztables.NewServiceClientFromConnectionString(connectionString, nil) } else { // Token-based access using Managed Identity client, err = aztables.NewServiceClient(tableEndpoint, nil, nil) } if err != nil { panic(err) }
// List tables pager := client.NewListTablesPager(nil) for pager.More() { page, err := pager.NextPage(context.Background()) if err != nil { panic(err) } for _, table := range page.Value { _ = table } }}Install the azure-data-tables package:
pip install azure-data-tablesRead the injected environment variables and connect:
import osfrom azure.data.tables import TableServiceClient
connection_string = os.getenv("TABLES_CONNECTIONSTRING")table_endpoint = os.getenv("TABLES_TABLEENDPOINT")
if connection_string: # Emulator or key-based access client = TableServiceClient.from_connection_string(connection_string)else: # Token-based access using Managed Identity from azure.identity import DefaultAzureCredential client = TableServiceClient(endpoint=table_endpoint, credential=DefaultAzureCredential())
# List tablesfor table in client.list_tables(): print(table.name)Install the @azure/data-tables package:
npm install @azure/data-tablesRead the injected environment variables and connect:
import { TableServiceClient } from '@azure/data-tables';
const connectionString = process.env.TABLES_CONNECTIONSTRING;const tableEndpoint = process.env.TABLES_TABLEENDPOINT;
let client: TableServiceClient;
if (connectionString) { // Emulator or key-based access client = TableServiceClient.fromConnectionString(connectionString);} else { // Token-based access using Managed Identity const { DefaultAzureCredential } = await import('@azure/identity'); client = new TableServiceClient(tableEndpoint!, new DefaultAzureCredential());}
// List tablesfor await (const table of client.listTables()) { console.log(table.name);}