Azure cosmosdb 使用自定义CosmosSerializer时GetItemQueryIterator出错

Azure cosmosdb 使用自定义CosmosSerializer时GetItemQueryIterator出错,azure-cosmosdb,azure-cosmosdb-sqlapi,Azure Cosmosdb,Azure Cosmosdb Sqlapi,我正在尝试将System.Text.Json序列化用于我正在进行的项目。当我使用自定义CosmosSerializer并调用GetItemQueryIterator时,ToStream调用将发送一个无法序列化的Microsoft.Azure.Cosmos.SqlQuerySpec。这是一个很容易重现问题的示例。感谢您的任何帮助 using Microsoft.Azure.Cosmos; using System; using System.IO; using System.Text.Json.S

我正在尝试将System.Text.Json序列化用于我正在进行的项目。当我使用自定义CosmosSerializer并调用GetItemQueryIterator时,ToStream调用将发送一个无法序列化的Microsoft.Azure.Cosmos.SqlQuerySpec。这是一个很容易重现问题的示例。感谢您的任何帮助

using Microsoft.Azure.Cosmos;
using System;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace CosmosTestApp
{
    class Program
    {
        const string endpoint = "<REPLACE>";
        const string key = "<REPLACE>";
        const string dbName = "test";
        const string containerName = "items";

        public static async Task Main(string[] args)
        {
            var options = new CosmosClientOptions()
            {
                Serializer = new CosmosJsonSerializer()
            };
            var client = new CosmosClient(endpoint, key, options);

            var dbResponse = await client.CreateDatabaseIfNotExistsAsync(dbName);
            var db = dbResponse.Database;
            var containerDef = new ContainerProperties(containerName, "/id");
            var containerResposne = await db.CreateContainerIfNotExistsAsync(containerDef);
            var testContainer = containerResposne.Container;

            var testDoc = new TestDoc();
            var docResponse = await testContainer.CreateItemAsync(testDoc, new PartitionKey(testDoc.Id));
            Console.WriteLine($"Created document {docResponse.Resource.Id}");

            var query = testContainer.GetItemQueryIterator<TestDoc>("SELECT * FROM c");
            while (query.HasMoreResults)
            {
                var doc = await query.ReadNextAsync();
                foreach(var x in doc.Resource)
                {
                    Console.WriteLine($"Retrieved document {x.Id}");
                }
            }
        }
    }

    internal class CosmosJsonSerializer : CosmosSerializer
    {
        public override T FromStream<T>(Stream stream)
        {
            using (var memoryStream = new MemoryStream())
            {
                stream.CopyTo(memoryStream);
                stream.Close();
                var item = JsonSerializer.Parse<T>(memoryStream.ToArray());
                return item;
            }
        }

        //This errors on the SqlQuerySpec 
        public override Stream ToStream<T>(T input)
            => new MemoryStream(JsonSerializer.ToUtf8Bytes(input));

    }

    internal class TestDoc
    {
        [JsonPropertyName("id")]
        public string Id { get; set; } = "1";

        public string TestString { get; set; } = "testing CosmosJsonSerializer";
    }

}
使用Microsoft.Azure.Cosmos;
使用制度;
使用System.IO;
使用System.Text.Json.Serialization;
使用System.Threading.Tasks;
命名空间CosmosTestApp
{
班级计划
{
常量字符串端点=”;
常量字符串键=”;
常量字符串dbName=“test”;
常量字符串containerName=“items”;
公共静态异步任务主(字符串[]args)
{
var options=new-cosmsclientoptions()
{
Serializer=new-CosmosJsonSerializer()
};
var client=新的CosmosClient(端点、键、选项);
var dbResponse=await client.CreateDatabaseIfNotExistsAsync(dbName);
var db=dbResponse.Database;
var containerDef=新的ContainerProperty(containerName,“/id”);
var containerResposne=await db.CreateContainerIfNotExistsAsync(containerDef);
var testContainer=containerResposne.Container;
var testDoc=新testDoc();
var docResponse=await testContainer.CreateItemAsync(testDoc,new PartitionKey(testDoc.Id));
WriteLine($“创建的文档{docResponse.Resource.Id}”);
var query=testContainer.getItemQueryInterator(“从c中选择*);
while(query.HasMoreResults)
{
var doc=wait query.ReadNextAsync();
foreach(文档资源中的变量x)
{
WriteLine($“检索到的文档{x.Id}”);
}
}
}
}
内部类CosmosJsonSerializer:CosmosSerializer
{
公共覆盖T FromStream(流)
{
使用(var memoryStream=new memoryStream())
{
stream.CopyTo(memoryStream);
stream.Close();
var item=JsonSerializer.Parse(memoryStream.ToArray());
退货项目;
}
}
//这会在SqlQuerySpec上显示错误
公共覆盖流到流(T输入)
=>新内存流(JsonSerializer.ToUtf8Bytes(输入));
}
内部类TestDoc
{
[JsonPropertyName(“id”)]
公共字符串Id{get;set;}=“1”;
公共字符串TestString{get;set;}=“testing CosmosJsonSerializer”;
}
}
编辑:错误已在此处存档并确认: