Cosmos DB EF ReadItemAsync 出现异常响应状态代码不表示成功:未授权(401);

Cosmos DB EF ReadItemAsync exception occurs Response status code does not indicate success: Unauthorized (401);

我正在执行的命令:

var feature = await container.ReadItemAsync<CosmosNormalizedFeatureModel>(guid, new Microsoft.Azure.Cosmos.PartitionKey(partitionKey));

抛出异常:

Response status code does not indicate success: Unauthorized (401); Substatus: 0; ActivityId: ; Reason: ();

我不相信这是真的,但我也没看出有什么不妥。

不确定我还能检查什么来解决问题。谢谢!

堆栈跟踪

at Microsoft.Azure.Cosmos.ResponseMessage.EnsureSuccessStatusCode()
   at Microsoft.Azure.Cosmos.CosmosResponseFactoryCore.ProcessMessage[T](ResponseMessage responseMessage, Func`2 createResponse)
   at Microsoft.Azure.Cosmos.CosmosResponseFactoryCore.CreateItemResponse[T](ResponseMessage responseMessage)
   at Microsoft.Azure.Cosmos.ContainerCore.<ReadItemAsync>d__56`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Azure.Cosmos.ClientContextCore.<RunWithDiagnosticsHelperAsync>d__38`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.Azure.Cosmos.ClientContextCore.<OperationHelperWithRootTraceAsync>d__29`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at xxx.<GetFeatureByGuid>d__7.MoveNext() in D:\xxx.cs:line 183
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at xxx.<GetNormalizedILIReportFeatureByGuid>d__10.MoveNext() in D:\xxx.cs:line 280

用于连接cosmos的密钥是读取密钥,读取的点似乎需要read/write密钥。

根据评论 - 您正在使用 Bulk mode

使用批量模式时,将操作打包在一起以提高网络性能(操作类型无关,读取操作可以与写入操作打包)并作为单个负载发送到后端。

负载是不同的类型,调用不同的 API(因此后端可以解压它们并处理它们,return 一个打包的响应)。

这个API使用Write键(因为包内可能有任何类型的操作)。您使用只读键的事实是导致 401 的原因。理想情况下,后端应该在错误中更明确 returns 虽然。