CosmosDB System.ArgumentNullException:'值不能为空。 (参数‘authKeyOrResourceToken’)”

发布于 2025-01-15 18:21:36 字数 1312 浏览 0 评论 0原文

目前,我在尝试连接到我的 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 看起来像这样: 输入图片此处的描述

此外,错误消息还包含另一个描述:

此异常最初是在此调用堆栈中引发的: [外部代码] Program.cs 中的 Program.$.__InitializeCosmosClientInstanceAsync|0_0(Microsoft.Extensions.Configuration.IConfigurationSection) [外部代码] Program.cs 中的 Program.$(string[])

Currently I am getting the following error while trying to connect to my CosmosDB:

System.ArgumentNullException: 'Value cannot be null. (Parameter 'authKeyOrResourceToken')'

If I debug then I can see that section variable is null so I don't know why it is null because I call the section by the configuration with the exact same name of my appsettings.json

The error occurs in the following line of code:

var section = builder.Configuration.GetSection("CosmosDb");
builder.Services.AddSingleton<ICosmosDbService>(
    InitializeCosmosClientInstanceAsync(section).GetAwaiter().GetResult());

The InitializeCosmosClientInstanceAsync method looks like that:
enter image description here

In my Visual Studio it looks like that:
description

My appsettings.json looks like that:
enter image description here

Additionally the error message contains another description:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2025-01-22 18:21:36

在 .NET6 中,重要的是要考虑到您必须根据 .NET6 编码指南重构文档 (.NET5) 中的代码!

我通过重构值来修复它的参数。如果我使用 appsettings.json,那么我还必须在 program.cs 中调用这些变量。

新程序.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());

IN .NET6 IT IS IMPORTANT TO CONSIDER THAT THERE YOU HAVE TO REFACTOR CODE FROM DOCUMENTATION (.NET5) ACCORDING TO THE .NET6 CODING GUIDLINE!

I fixed it by refactor the value of the parameters. If I use an appsettings.json then I also have to call these variables in my program.cs.

New 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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文