-
添加Nuget包
Azure.Data.Tables
-
在
appsettings.json
中配置连接字符串
-
在Module中获取连接字符串,并注册服务
private void ConfigurationTableStorage(ServiceConfigurationContext context)
{
var connectionString = context.Services.GetConfiguration()["TABLE_CONNECTION_STRING:Default"];
TableServiceClient tableServiceClient = new TableServiceClient(connectionString);
context.Services.AddSingleton(tableServiceClient);
}
context.Services.AddSingleton
方法用于将一个服务类型注册为单例模式,以确保在整个应用程序生命周期内只创建一个实例。每次需要 TableServiceClient 的实例时,都会返回同一个实例。
- 在Service中注入TableServiceClient
public class TableStorageAppService : TableStorageManagementAppService, ITableStorageAppService
{
private readonly TableServiceClient tableClient;
public TableStorageAppService(TableServiceClient tableClient)
{
this.tableClient = tableClient;
}
public async Task TestGetStorageData()
{
var table = tableClient.GetTableClient("realtime");
var result = await table.GetEntityAsync<TestObj>("TestPar", "TestRow");
Logger.LogError($"==================>{JsonConvert.SerializeObject(result)}");
}
}
运行结果
[09:26:46 ERR] ==================>{"Value":{"PartitionKey":"TestPar","RowKey":"TestRow","Timestamp":"2023-12-25T08:56:36.483685+00:00","TestColumn":"123456","ETag":{}},"HasValue":true}
标签:集成,TableStorage,TableServiceClient,Abp,var,context,Services,tableClient,public
From: https://www.cnblogs.com/kelvinxiong/p/17927491.html