CosmosDB System.ArgumentNullException:'值不能为空。 (参数‘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
方法如下所示:
此外,错误消息还包含另一个描述:
此异常最初是在此调用堆栈中引发的: [外部代码] 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:
In my Visual Studio it looks like that:
My appsettings.json
looks like that:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 .NET6 中,重要的是要考虑到您必须根据 .NET6 编码指南重构文档 (.NET5) 中的代码!
我通过重构值来修复它的参数。如果我使用 appsettings.json,那么我还必须在 program.cs 中调用这些变量。
新程序.cs:
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: