From f75d8057b38ae212c838fd7e5a18e32dae58fcf5 Mon Sep 17 00:00:00 2001 From: petero-dk <2478689+petero-dk@users.noreply.github.com> Date: Mon, 19 Aug 2024 22:20:05 +0200 Subject: [PATCH 1/3] Implement TableServiceClient --- .../StorageContext.cs | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs b/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs index 4e6807d..409aac8 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs @@ -7,18 +7,25 @@ namespace CoreHelpers.WindowsAzure.Storage.Table public partial class StorageContext : IStorageContext { private IStorageContextDelegate _delegate { get; set; } - private string _connectionString; + private TableServiceClient tableServiceClient { get; set; } - public StorageContext(string storageAccountName, string storageAccountKey, string storageEndpointSuffix = null) + public StorageContext(string storageAccountName, string storageAccountKey, string storageEndpointSuffix = null) { - _connectionString = String.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}", "https", storageAccountName, storageAccountKey); - if (!String.IsNullOrEmpty(storageEndpointSuffix)) - _connectionString = String.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2};EndpointSuffix={3}", "https", storageAccountName, storageAccountKey, storageEndpointSuffix); + if (!String.IsNullOrEmpty(storageEndpointSuffix)) + tableServiceClient = new TableServiceClient(string.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2};EndpointSuffix={3}", "https", storageAccountName, storageAccountKey, storageEndpointSuffix)); + else + tableServiceClient = new TableServiceClient(string.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}", "https", storageAccountName, storageAccountKey)); + } public StorageContext(string connectionString) { - _connectionString = connectionString; + tableServiceClient = new TableServiceClient(connectionString); + } + + public StorageContext(TableServiceClient tableServiceClient) + { + this.tableServiceClient = tableServiceClient; } public StorageContext(StorageContext parentContext) @@ -32,8 +39,8 @@ public StorageContext(StorageContext parentContext) // take the tablename prefix _tableNamePrefix = parentContext._tableNamePrefix; - // store the connection string - _connectionString = parentContext._connectionString; + // store the connection string + tableServiceClient = parentContext.tableServiceClient; } public void Dispose() @@ -56,7 +63,7 @@ public TableClient GetTableClient() private TableClient GetTableClient(string tableName) { - return new TableClient(_connectionString, tableName); + return tableServiceClient.GetTableClient(tableName); } } } From 644e8d24708df3f0458f87a5587bec76db6fb7e8 Mon Sep 17 00:00:00 2001 From: petero-dk <2478689+petero-dk@users.noreply.github.com> Date: Mon, 19 Aug 2024 22:24:03 +0200 Subject: [PATCH 2/3] fix formatting --- .../StorageContext.cs | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs b/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs index 409aac8..1a1ade7 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table/StorageContext.cs @@ -5,18 +5,18 @@ namespace CoreHelpers.WindowsAzure.Storage.Table { public partial class StorageContext : IStorageContext - { - private IStorageContextDelegate _delegate { get; set; } - private TableServiceClient tableServiceClient { get; set; } + { + private IStorageContextDelegate _delegate { get; set; } + private TableServiceClient tableServiceClient { get; set; } public StorageContext(string storageAccountName, string storageAccountKey, string storageEndpointSuffix = null) - { + { if (!String.IsNullOrEmpty(storageEndpointSuffix)) tableServiceClient = new TableServiceClient(string.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2};EndpointSuffix={3}", "https", storageAccountName, storageAccountKey, storageEndpointSuffix)); else tableServiceClient = new TableServiceClient(string.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}", "https", storageAccountName, storageAccountKey)); - - } + + } public StorageContext(string connectionString) { @@ -29,41 +29,41 @@ public StorageContext(TableServiceClient tableServiceClient) } public StorageContext(StorageContext parentContext) - { + { // we reference the entity mapper _entityMapperRegistry = new Dictionary(parentContext._entityMapperRegistry); // we are using the delegate - this.SetDelegate(parentContext._delegate); + this.SetDelegate(parentContext._delegate); - // take the tablename prefix - _tableNamePrefix = parentContext._tableNamePrefix; + // take the tablename prefix + _tableNamePrefix = parentContext._tableNamePrefix; // store the connection string tableServiceClient = parentContext.tableServiceClient; } - + public void Dispose() - {} + { } + + public void SetDelegate(IStorageContextDelegate delegateModel) + => _delegate = delegateModel; - public void SetDelegate(IStorageContextDelegate delegateModel) - => _delegate = delegateModel; + public IStorageContextDelegate GetDelegate() + => _delegate; - public IStorageContextDelegate GetDelegate() - => _delegate; + public IStorageContext CreateChildContext() + => new StorageContext(this); - public IStorageContext CreateChildContext() - => new StorageContext(this); - public TableClient GetTableClient() { - var tableName = GetTableName(); - return GetTableClient(tableName); + var tableName = GetTableName(); + return GetTableClient(tableName); } private TableClient GetTableClient(string tableName) { - return tableServiceClient.GetTableClient(tableName); - } + return tableServiceClient.GetTableClient(tableName); + } } } From 7bb9d35e5ddf1def86501742c0b85b6e1498b582 Mon Sep 17 00:00:00 2001 From: petero-dk <2478689+petero-dk@users.noreply.github.com> Date: Mon, 19 Aug 2024 22:33:52 +0200 Subject: [PATCH 3/3] fix bug --- .../StorageContextTableManagement.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CoreHelpers.WindowsAzure.Storage.Table/StorageContextTableManagement.cs b/CoreHelpers.WindowsAzure.Storage.Table/StorageContextTableManagement.cs index 123dcfc..28d0d76 100644 --- a/CoreHelpers.WindowsAzure.Storage.Table/StorageContextTableManagement.cs +++ b/CoreHelpers.WindowsAzure.Storage.Table/StorageContextTableManagement.cs @@ -107,8 +107,7 @@ public async Task> QueryTableList() { var tables = new List(); - var tsc = new TableServiceClient(_connectionString); - var tablePages = tsc.QueryAsync().AsPages(); + var tablePages = tableServiceClient.QueryAsync().AsPages(); await foreach (var tablePage in tablePages) tables.AddRange(tablePage.Values.Select(t => t.Name));