CosmosDB System.ArgumentNullException:“值不能为空。 (参数'authKeyOrResourceToken')'
CosmosDB System.ArgumentNullException: 'Value cannot be null. (Parameter 'authKeyOrResourceToken')'
目前我在尝试连接到我的 CosmosDB 时遇到以下错误:
System.ArgumentNullException: 'Value cannot be null. (Parameter 'authKeyOrResourceToken')'
如果我调试然后我可以看到该部分变量是 null
所以我不知道为什么它是 null
因为我通过配置调用该部分与我的完全相同的名称appsettings.json
错误发生在以下代码行:
var section = builder.Configuration.GetSection("CosmosDb");
builder.Services.AddSingleton<ICosmosDbService>(
InitializeCosmosClientInstanceAsync(section).GetAwaiter().GetResult());
InitializeCosmosClientInstanceAsync
方法如下所示:
在我的 Visual Studio 中它看起来像这样:
我的 appsettings.json
看起来像这样:
此外,错误消息包含另一个描述:
This exception was originally thrown at this call stack:
[External Code]
Program.$.__InitializeCosmosClientInstanceAsync|0_0(Microsoft.Extensions.Configuration.IConfigurationSection) in Program.cs
[External Code]
Program.$(string[]) in Program.cs
在 .NET6 中,重要的是要考虑到您必须根据 .NET6 编码指南重构文档 (.NET5) 中的代码!
我通过重构参数值修复了它。如果我使用 appsettings.json,那么我还必须在我的 program.cs.
中调用这些变量
新 program.cs:
async Task<CosmosDbService> InitializeCosmosClientInstanceAsync(
IConfigurationSection configurationSection) {
var databaseName = configurationSection.GetSection("DatabaseName").Value;
var containerName = configurationSection.GetSection("ContainerName").Value;
var accountUri = configurationSection.GetSection("Account").Value;
var primaryKey = configurationSection.GetSection("authKey").Value;
var client = new CosmosClient(accountUri, primaryKey);
var cosmosDbService = new CosmosDbService(client, databaseName, containerName);
var database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");
return cosmosDbService;
}
builder.Services.AddSingleton<ICosmosDbService>(InitializeCosmosClientInstanceAsync(
builder.Configuration.GetSection("CosmosDb")).GetAwaiter().GetResult());
目前我在尝试连接到我的 CosmosDB 时遇到以下错误:
System.ArgumentNullException: 'Value cannot be null. (Parameter 'authKeyOrResourceToken')'
如果我调试然后我可以看到该部分变量是 null
所以我不知道为什么它是 null
因为我通过配置调用该部分与我的完全相同的名称appsettings.json
错误发生在以下代码行:
var section = builder.Configuration.GetSection("CosmosDb");
builder.Services.AddSingleton<ICosmosDbService>(
InitializeCosmosClientInstanceAsync(section).GetAwaiter().GetResult());
InitializeCosmosClientInstanceAsync
方法如下所示:
在我的 Visual Studio 中它看起来像这样:
我的 appsettings.json
看起来像这样:
此外,错误消息包含另一个描述:
This exception was originally thrown at this call stack: [External Code] Program.$.__InitializeCosmosClientInstanceAsync|0_0(Microsoft.Extensions.Configuration.IConfigurationSection) in Program.cs [External Code] Program.$(string[]) in Program.cs
在 .NET6 中,重要的是要考虑到您必须根据 .NET6 编码指南重构文档 (.NET5) 中的代码!
我通过重构参数值修复了它。如果我使用 appsettings.json,那么我还必须在我的 program.cs.
中调用这些变量新 program.cs:
async Task<CosmosDbService> InitializeCosmosClientInstanceAsync(
IConfigurationSection configurationSection) {
var databaseName = configurationSection.GetSection("DatabaseName").Value;
var containerName = configurationSection.GetSection("ContainerName").Value;
var accountUri = configurationSection.GetSection("Account").Value;
var primaryKey = configurationSection.GetSection("authKey").Value;
var client = new CosmosClient(accountUri, primaryKey);
var cosmosDbService = new CosmosDbService(client, databaseName, containerName);
var database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");
return cosmosDbService;
}
builder.Services.AddSingleton<ICosmosDbService>(InitializeCosmosClientInstanceAsync(
builder.Configuration.GetSection("CosmosDb")).GetAwaiter().GetResult());