我在使用 CosmosDB SDK V4.0.0-preview3 时遇到问题。当我尝试更改默认的 CosmosSerializationOptions
时,当我尝试使用 GetItemQueryIterator
时,它会导致异常。除了这个方法之外,其他一切都工作正常。
var client = new CosmosClientBuilder(connectionString)
.WithSerializerOptions(new CosmosSerializationOptions { PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase })
.Build();
var container = client.GetContainer("SocialNetwork", "Posts");
var query = new QueryDefinition("SELECT * FROM c");
await foreach (var item in container.GetItemQueryIterator<Post>(query))
{
Console.WriteLine(item);
}
异常消息:
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source')
at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Azure.Cosmos.CosmosResponseFactory.CreateQueryFeedResponseWithSerializer[T](Response cosmosResponseMessage, CosmosSerializer serializer)
at Azure.Cosmos.CosmosResponseFactory.CreateQueryFeedResponse[T](Response cosmosResponseMessage)
at Azure.Cosmos.PageIteratorCore`1.GetPageAsync(String continuation, CancellationToken cancellationToken)
at Azure.Cosmos.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+MoveNext()
at Azure.Cosmos.PageResponseEnumerator.FuncAsyncPageable`1.AsPages(String continuationToken, Nullable`1 pageSizeHint)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext()
at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext()
at Azure.AsyncPageable`1.GetAsyncEnumerator(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
at ConsoleAppCosmosDb.Program.Main(String[] args) in C:\Users\XPS\source\repos\ConsoleAppCosmosDb\ConsoleAppCosmosDb\Program.cs:line 29
at ConsoleAppCosmosDb.Program.Main(String[] args) in C:\Users\XPS\source\repos\ConsoleAppCosmosDb\ConsoleAppCosmosDb\Program.cs:line 29
at ConsoleAppCosmosDb.Program.<Main>(String[] args)
如果我删除此选项或将 CamelCase
更改为 Default
一切正常。
最佳答案
如 https://github.com/Azure/azure-cosmos-dotnet-v3/issues/1193 中所述,这似乎是预览版 V4 版本上的一个错误。
主要与V4预览状态有关,部分内部连线待移植。
您目前可以通过在 Post
类上使用 JsonPropertyName
并在其中使用 Camel Case 来解决此问题。
关于azure - 将默认 CosmosSerializationOptions 更改为驼峰命名法会导致 CosmosDB SDK V4 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014907/