.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
- Azure subscription - create one for free
- An Azure storage account or Azure Cosmos DB database with Azure Table API specified. - create a storage account
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 --prerelease
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 AddAzureTableService extension to register a TableServiceClient for use via the dependency injection container.
builder.AddAzureTableService("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
The .NET Aspire Azure hosting APIs are available in the Aspire.Hosting.Azure NuGet package. These APIs provide Azure-specific resources used within the App Host. To add Azure resources to your IDistributedApplicationBuilder, install the Aspire.Hosting.Azure NuGet package.
dotnet add package Aspire.Hosting.Azure --prerelease
In your app host project, register the Azure Table Storage component and consume the service using the following methods:
// Service registration
var tables = builder.AddAzureStorage("storage")
.AddTables("tables");
// Service consumption
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",
"HealthChecks": false,
"Tracing": true,
"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 AddAzureTableService 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 Namespace:
builder.AddAzureTableService(
"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 AddAzureTableService method. For example to set the TableServiceClient ID to identify the client:
builder.AddAzureTableService(
"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 AddAzureTableService method and passing in the INSTANCE_NAME.
builder.AddAzureTableService("INSTANCE_NAME");
The corresponding configuration JSON is defined as follows:
{
"Aspire":{
"Azure": {
"Data": {
"Tables": {
"INSTANCE_NAME": {
"ServiceUri": "YOUR_URI",
"HealthChecks": false,
"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. |
HealthChecks |
A boolean value that indicates whether the Table Storage health check is enabled or not. |
Tracing |
A boolean value that indicates whether the OpenTelemetry tracing is enabled 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
AzureTableStorageHealthCheckhealth check, which attempts to connect to and query table storage - Integrates with the
/healthHTTP 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.CoreAzure.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
.NET Aspire
Feedback
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback.
Submit and view feedback for