Skip to content

Commit e716d01

Browse files
author
Changjian Wang
committed
Refactor configuration handling to use AZURE_CONTENT_UNDERSTANDING_ENDPOINT and AZURE_APIVERSION across multiple services and tests
1 parent b65a8a6 commit e716d01

File tree

18 files changed

+153
-82
lines changed

18 files changed

+153
-82
lines changed

AnalyzerTraining/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ public static async Task Main(string[] args)
1515
var host = Host.CreateDefaultBuilder(args)
1616
.ConfigureServices((context, services) =>
1717
{
18-
if (string.IsNullOrWhiteSpace(context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint")))
18+
string endpoint = context.Configuration.GetValue<string>("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? string.Empty;
19+
if (string.IsNullOrWhiteSpace(endpoint))
1920
{
2021
throw new ArgumentException("Endpoint must be provided in appsettings.json.");
2122
}
2223

23-
if (string.IsNullOrWhiteSpace(context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion")))
24+
string apiVersion = context.Configuration.GetValue<string>("AZURE_APIVERSION") ?? string.Empty;
25+
if (string.IsNullOrWhiteSpace(apiVersion))
2426
{
2527
throw new ArgumentException("API version must be provided in appsettings.json.");
2628
}
2729

2830
services.AddConfigurations(opts =>
2931
{
30-
context.Configuration.GetSection("AZURE_CU_CONFIG").Bind(opts);
32+
opts.Endpoint = endpoint;
33+
opts.ApiVersion = apiVersion;
34+
opts.SubscriptionKey = context.Configuration.GetValue<string>("AZURE_SUBSCRIPTION_ID") ?? string.Empty;
35+
3136
// This header is used for sample usage telemetry, please comment out this line if you want to opt out.
3237
opts.UserAgent = "azure-ai-content-understanding-dotnet/analyzer_training";
3338
});

AzureAiContentUnderstanding.Tests/AnalyzerTrainingIntegrationTest.cs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,18 @@ public class AnalyzerTrainingIntegrationTest
2828
public AnalyzerTrainingIntegrationTest()
2929
{
3030
var host = Host.CreateDefaultBuilder()
31+
.ConfigureAppConfiguration((context, config) =>
32+
{
33+
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
34+
config.AddEnvironmentVariables();
35+
})
3136
.ConfigureServices((context, services) =>
3237
{
33-
// --- DEBUG: 检查环境变量与配置的可见性(临时,运行后可移除) ---
34-
var envRaw = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT");
35-
var configValue = context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint");
36-
var cuEnvVar = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG__Endpoint");
37-
38-
Console.WriteLine("DEBUG: AZURE_CONTENT_UNDERSTANDING_ENDPOINT env: " + (string.IsNullOrEmpty(envRaw) ? "<EMPTY>" : "<SET>"));
39-
Console.WriteLine("DEBUG: AZURE_CU_CONFIG:Endpoint from IConfiguration: " + (string.IsNullOrEmpty(configValue) ? "<EMPTY>" : "<SET>"));
40-
Console.WriteLine("DEBUG: AZURE_CU_CONFIG__Endpoint env (double-underscore): " + (string.IsNullOrEmpty(cuEnvVar) ? "<EMPTY>" : "<SET>"));
41-
42-
if (!string.IsNullOrEmpty(envRaw))
43-
{
44-
try
45-
{
46-
var uri = new Uri(envRaw);
47-
Console.WriteLine("DEBUG: AZURE_CONTENT_UNDERSTANDING_ENDPOINT host: " + uri.Host);
48-
}
49-
catch
50-
{
51-
Console.WriteLine("DEBUG: AZURE_CONTENT_UNDERSTANDING_ENDPOINT host: (could not parse)");
52-
}
53-
}
54-
// --- END DEBUG ---
55-
5638
// Load configuration from environment variables or appsettings.json
57-
string? endpoint = envRaw ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint");
39+
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? context.Configuration.GetValue<string>("AZURE_CONTENT_UNDERSTANDING_ENDPOINT");
5840

5941
// API version for Azure Content Understanding service
60-
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion");
42+
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_APIVERSION");
6143

6244
if (string.IsNullOrWhiteSpace(endpoint))
6345
{
@@ -69,10 +51,10 @@ public AnalyzerTrainingIntegrationTest()
6951
}
7052

7153
// account name
72-
accountName = Environment.GetEnvironmentVariable("TRAINING_DATA_STORAGE_ACCOUNT_NAME") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:TrainingDataStorageAccountName") ?? "";
54+
accountName = Environment.GetEnvironmentVariable("TRAINING_DATA_STORAGE_ACCOUNT_NAME") ?? context.Configuration.GetValue<string>("TRAINING_DATA_STORAGE_ACCOUNT_NAME") ?? "";
7355

7456
// container name
75-
containerName = Environment.GetEnvironmentVariable("TRAINING_DATA_CONTAINER_NAME") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:TrainingDataContainerName") ?? "";
57+
containerName = Environment.GetEnvironmentVariable("TRAINING_DATA_CONTAINER_NAME") ?? context.Configuration.GetValue<string>("TRAINING_DATA_CONTAINER_NAME") ?? "";
7658

7759
if (string.IsNullOrWhiteSpace(accountName))
7860
{
@@ -88,6 +70,7 @@ public AnalyzerTrainingIntegrationTest()
8870
{
8971
opts.Endpoint = endpoint;
9072
opts.ApiVersion = apiVersion;
73+
9174
// This header is used for sample usage telemetry, please comment out this line if you want to opt out.
9275
opts.UserAgent = "azure-ai-content-understanding-dotnet/analyzer_training";
9376
});

AzureAiContentUnderstanding.Tests/BuildPersonDirectoryIntegrationTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,23 @@ public class BuildPersonDirectoryIntegrationTest
2424
/// Sets up dependency injection, configures the test host, and validates required configurations.
2525
/// </summary>
2626
/// <exception cref="ArgumentException">
27-
/// Thrown if required configuration values for AZURE_CU_CONFIG:Endpoint or AZURE_CU_CONFIG:ApiVersion are missing.
27+
/// Thrown if required configuration values for AZURE_CONTENT_UNDERSTANDING_ENDPOINT or AZURE_APIVERSION are missing.
2828
/// </exception>
2929
public BuildPersonDirectoryIntegrationTest()
3030
{
3131
var host = Host.CreateDefaultBuilder()
32+
.ConfigureAppConfiguration((context, config) =>
33+
{
34+
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
35+
config.AddEnvironmentVariables();
36+
})
3237
.ConfigureServices((context, services) =>
3338
{
3439
// Load configuration from environment variables or appsettings.json
35-
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG_Endpoint") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint");
40+
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? context.Configuration.GetValue<string>("AZURE_CONTENT_UNDERSTANDING_ENDPOINT");
3641

3742
// API version for Azure Content Understanding service
38-
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG_ApiVersion") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion");
43+
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_APIVERSION");
3944

4045
if (string.IsNullOrWhiteSpace(endpoint))
4146
{

AzureAiContentUnderstanding.Tests/ClassifierIntegrationTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ public class ClassifierIntegrationTest
2222
/// Sets up dependency injection, configures the test host, and validates required configurations for classifier testing.
2323
/// </summary>
2424
/// <exception cref="ArgumentException">
25-
/// Thrown if required configuration values for "AZURE_CU_CONFIG:Endpoint" or "AZURE_CU_CONFIG:ApiVersion" are missing.
25+
/// Thrown if required configuration values for "AZURE_CONTENT_UNDERSTANDING_ENDPOINT" or "AZURE_APIVERSION" are missing.
2626
/// </exception>
2727
public ClassifierIntegrationTest()
2828
{
2929
var host = Host.CreateDefaultBuilder()
30+
.ConfigureAppConfiguration((context, config) =>
31+
{
32+
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
33+
config.AddEnvironmentVariables();
34+
})
3035
.ConfigureServices((context, services) =>
3136
{
3237
// Load configuration from environment variables or appsettings.json
33-
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG_Endpoint") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint");
38+
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? context.Configuration.GetValue<string>("AZURE_CONTENT_UNDERSTANDING_ENDPOINT");
3439

3540
// API version for Azure Content Understanding service
36-
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG_ApiVersion") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion");
41+
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_APIVERSION");
3742

3843
if (string.IsNullOrWhiteSpace(endpoint))
3944
{

AzureAiContentUnderstanding.Tests/ContentExtractionIntegrationTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,23 @@ public class ContentExtractionIntegrationTest
2121
/// Sets up dependency injection, configures the test host, and validates required configurations.
2222
/// </summary>
2323
/// <exception cref="ArgumentException">
24-
/// Thrown if required configuration values for "AZURE_CU_CONFIG:Endpoint" or "AZURE_CU_CONFIG:ApiVersion" are missing.
24+
/// Thrown if required configuration values for "AZURE_CONTENT_UNDERSTANDING_ENDPOINT" or "AZURE_APIVERSION" are missing.
2525
/// </exception>
2626
public ContentExtractionIntegrationTest()
2727
{
2828
var host = Host.CreateDefaultBuilder()
29+
.ConfigureAppConfiguration((context, config) =>
30+
{
31+
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
32+
config.AddEnvironmentVariables();
33+
})
2934
.ConfigureServices((context, services) =>
3035
{
3136
// Load configuration from environment variables or appsettings.json
32-
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG_Endpoint") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint");
37+
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? context.Configuration.GetValue<string>("AZURE_CONTENT_UNDERSTANDING_ENDPOINT");
3338

3439
// API version for Azure Content Understanding service
35-
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG_ApiVersion") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion");
40+
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_APIVERSION");
3641

3742
if (string.IsNullOrWhiteSpace(endpoint))
3843
{

AzureAiContentUnderstanding.Tests/ConversationalFieldExtractionIntegrationTest.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,32 @@ public class ConversationalConversationalFieldExtractionIntegrationTest
1616
public ConversationalConversationalFieldExtractionIntegrationTest()
1717
{
1818
var host = Host.CreateDefaultBuilder()
19+
.ConfigureAppConfiguration((context, config) =>
20+
{
21+
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
22+
config.AddEnvironmentVariables();
23+
})
1924
.ConfigureServices((context, services) =>
2025
{
21-
if (string.IsNullOrWhiteSpace(context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint")))
26+
// Load configuration from environment variables or appsettings.json
27+
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? context.Configuration.GetValue<string>("AZURE_CONTENT_UNDERSTANDING_ENDPOINT");
28+
29+
// API version for Azure Content Understanding service
30+
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_APIVERSION");
31+
32+
if (string.IsNullOrWhiteSpace(endpoint))
2233
{
23-
throw new ArgumentException("Endpoint must be provided in appsettings.json.");
34+
throw new ArgumentException("Endpoint must be provided in environment variable or appsettings.json.");
2435
}
25-
if (string.IsNullOrWhiteSpace(context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion")))
36+
if (string.IsNullOrWhiteSpace(apiVersion))
2637
{
27-
throw new ArgumentException("API version must be provided in appsettings.json.");
38+
throw new ArgumentException("API version must be provided in environment variable or appsettings.json.");
2839
}
40+
2941
services.AddConfigurations(opts =>
3042
{
31-
context.Configuration.GetSection("AZURE_CU_CONFIG").Bind(opts);
43+
opts.Endpoint = endpoint;
44+
opts.ApiVersion = apiVersion;
3245
// This header is used for sample usage telemetry, please comment out this line if you want to opt out.
3346
opts.UserAgent = "azure-ai-content-understanding-dotnet/conversational_field_extraction";
3447
});

AzureAiContentUnderstanding.Tests/FieldExtractionIntegrationTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ public class FieldExtractionIntegrationTest
2222
/// Sets up dependency injection, configures the test host, and validates required configurations for field extraction.
2323
/// </summary>
2424
/// <exception cref="ArgumentException">
25-
/// Thrown if required configuration values for "AZURE_CU_CONFIG:Endpoint" or "AZURE_CU_CONFIG:ApiVersion" are missing.
25+
/// Thrown if required configuration values for "AZURE_CONTENT_UNDERSTANDING_ENDPOINT" or "AZURE_APIVERSION" are missing.
2626
/// </exception>
2727
public FieldExtractionIntegrationTest()
2828
{
2929
var host = Host.CreateDefaultBuilder()
30+
.ConfigureAppConfiguration((context, config) =>
31+
{
32+
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
33+
config.AddEnvironmentVariables();
34+
})
3035
.ConfigureServices((context, services) =>
3136
{
3237
// Load configuration from environment variables or appsettings.json
33-
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG_Endpoint") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint");
38+
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? context.Configuration.GetValue<string>("AZURE_CONTENT_UNDERSTANDING_ENDPOINT");
3439

3540
// API version for Azure Content Understanding service
36-
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_CU_CONFIG_ApiVersion") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion");
41+
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_APIVERSION");
3742

3843
if (string.IsNullOrWhiteSpace(endpoint))
3944
{

AzureAiContentUnderstanding.Tests/FieldExtractionProModeIntegrationTest.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@ public class FieldExtractionProModeIntegrationTest
2828
/// Sets up dependency injection, configures the test host, and validates required configurations.
2929
/// </summary>
3030
/// <exception cref="ArgumentException">
31-
/// Thrown if required configuration values for "AZURE_CU_CONFIG:Endpoint" or "AZURE_CU_CONFIG:ApiVersion" are missing.
31+
/// Thrown if required configuration values for "AZURE_CONTENT_UNDERSTANDING_ENDPOINT" or "AZURE_APIVERSION" are missing.
3232
/// </exception>
3333
public FieldExtractionProModeIntegrationTest()
3434
{
3535
var host = Host.CreateDefaultBuilder()
36+
.ConfigureAppConfiguration((context, config) =>
37+
{
38+
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
39+
config.AddEnvironmentVariables();
40+
})
3641
.ConfigureServices((context, services) =>
3742
{
3843
// Load configuration from environment variables or appsettings.json
39-
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:Endpoint");
44+
string? endpoint = Environment.GetEnvironmentVariable("AZURE_CONTENT_UNDERSTANDING_ENDPOINT") ?? context.Configuration.GetValue<string>("AZURE_CONTENT_UNDERSTANDING_ENDPOINT");
4045

4146
// API version for Azure Content Understanding service
42-
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ApiVersion");
47+
string? apiVersion = Environment.GetEnvironmentVariable("AZURE_APIVERSION") ?? context.Configuration.GetValue<string>("AZURE_APIVERSION");
4348

4449
if (string.IsNullOrWhiteSpace(endpoint))
4550
{
@@ -51,10 +56,10 @@ public FieldExtractionProModeIntegrationTest()
5156
}
5257

5358
// account name
54-
accountName = Environment.GetEnvironmentVariable("REFERENCE_DOC_STORAGE_ACCOUNT_NAME") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ReferenceDocStorageAccountName") ?? "";
59+
accountName = Environment.GetEnvironmentVariable("REFERENCE_DOC_STORAGE_ACCOUNT_NAME") ?? context.Configuration.GetValue<string>("REFERENCE_DOC_STORAGE_ACCOUNT_NAME") ?? "";
5560

5661
// container name
57-
containerName = Environment.GetEnvironmentVariable("REFERENCE_DOC_CONTAINER_NAME") ?? context.Configuration.GetValue<string>("AZURE_CU_CONFIG:ReferenceDocContainerName") ?? "";
62+
containerName = Environment.GetEnvironmentVariable("REFERENCE_DOC_CONTAINER_NAME") ?? context.Configuration.GetValue<string>("REFERENCE_DOC_CONTAINER_NAME") ?? "";
5863

5964
if (string.IsNullOrWhiteSpace(accountName))
6065
{

0 commit comments

Comments
 (0)