Skip to content

Commit 2e267c1

Browse files
committed
add AddTableStorageService
Related to: aliencube#208, aliencube#213
1 parent 1e932d5 commit 2e267c1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/AzureOpenAIProxy.ApiApp/AzureOpenAIProxy.ApiApp.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="Azure.AI.OpenAI" Version="$(AzureOpenAIVersion)" />
10+
<PackageReference Include="Azure.Data.Tables" Version="12.9.0" />
1011
<PackageReference Include="Azure.Identity" Version="1.12.0" />
1112
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
1213
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="$(AspNetCoreVersion)" />

src/AzureOpenAIProxy.ApiApp/Extensions/ServiceCollectionExtensions.cs

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Azure.Identity;
1+
using Azure.Data.Tables;
2+
using Azure.Identity;
23
using Azure.Security.KeyVault.Secrets;
34

45
using AzureOpenAIProxy.ApiApp.Builders;
@@ -134,4 +135,36 @@ public static IServiceCollection AddOpenApiService(this IServiceCollection servi
134135

135136
return services;
136137
}
138+
139+
/// <summary>
140+
/// Adds the TableServiceClient to the services collection.
141+
/// </summary>
142+
/// <param name="services"><see cref="IServiceCollection"/> instance.</param>
143+
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns>
144+
public static IServiceCollection AddTableStorageService(this IServiceCollection services)
145+
{
146+
services.AddSingleton<TableServiceClient>(sp => {
147+
var configuration = sp.GetService<IConfiguration>()
148+
?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered.");
149+
150+
var settings = configuration.GetSection(AzureSettings.Name).GetSection(StorageAccountSettings.Name).Get<StorageAccountSettings>()
151+
?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration.");
152+
153+
if (string.IsNullOrWhiteSpace(settings.ConnectionString) == true)
154+
{
155+
throw new InvalidOperationException($"{nameof(StorageAccountSettings.ConnectionString)} is not defined.");
156+
}
157+
158+
if (string.IsNullOrWhiteSpace(settings.ContainerName) == true)
159+
{
160+
throw new InvalidOperationException($"{nameof(StorageAccountSettings.ContainerName)} is not defined.");
161+
}
162+
163+
var client = new TableServiceClient(settings.ConnectionString);
164+
165+
return client;
166+
});
167+
168+
return services;
169+
}
137170
}

0 commit comments

Comments
 (0)