导致“InvalidCastExceptions”的多个异步Cosmos DB查询

问题描述 投票:1回答:1

我有一个Azure功能应用程序,其中运行多个功能。如果多个查询最终同时运行,我会从DocumentQuery代码的深层内部获得InvalidCastException。如果我禁用除了单个函数之外的所有函数(无关紧要),则不会发生错误,导致我相信存在某种共享内存问题。

除了有问题的查询之外,我已经删除了所有内容以尝试隔离问题,确认问题确实源于对cosmos Db的> 1异步调用。

如果我在评估另一个查询时调用DocumentQuery.ToString()await DocumentQuery.ExecuteNextAsync(),似乎会出现这个问题。具体来说,这里出现了所有问题

public static async Task<List<T>> ToListAsync<T>(this IQueryable<T> source, QueryCase queryCase = QueryCase.CamelCase) where T : class
{
    var results = new List<T>();
    IDocumentQuery<T> documentQuery = source.ChangeCase(queryCase).AsDocumentQuery();

    while (documentQuery.HasMoreResults)
    {
        FeedResponse<T> queryResult = await documentQuery.ExecuteNextAsync<T>();
        results.AddRange(queryResult);
    }

    return results;
}

ChangeCase()函数调用DocumentQuery.ToString()并且它将在那里失败,如果我删除它,失败将发生在ExecuteNextAsync()

堆栈跟踪:

System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast object of type 'Newtonsoft.Json.Converters.StringEnumConverter' to type 'Newtonsoft.Json.JsonConverter'.
  Source=Microsoft.Azure.DocumentDB.Core
  StackTrace:
   at Microsoft.Azure.Documents.Linq.ExpressionToSql.ApplyCustomConverters(Expression left, SqlLiteralScalarExpression right)
   at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitBinary(BinaryExpression inputExpression, TranslationContext context)
   at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitScalarExpression(Expression inputExpression, TranslationContext context)
   at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitScalarLambda(Expression inputExpression, TranslationContext context)
   at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitWhere(ReadOnlyCollection`1 arguments, TranslationContext context)
   at Microsoft.Azure.Documents.Linq.ExpressionToSql.VisitMethodCall(MethodCallExpression inputExpression, TranslationContext context)
   at Microsoft.Azure.Documents.Linq.ExpressionToSql.Translate(Expression inputExpression, TranslationContext context)
   at Microsoft.Azure.Documents.Linq.ExpressionToSql.TranslateQuery(Expression inputExpression)
   at Microsoft.Azure.Documents.Linq.SqlTranslator.TranslateQuery(Expression inputExpression)
   at Microsoft.Azure.Documents.Linq.DocumentQueryEvaluator.HandleMethodCallExpression(MethodCallExpression expression)
   at Microsoft.Azure.Documents.Linq.DocumentQueryEvaluator.Evaluate(Expression expression)
   at Microsoft.Azure.Documents.Linq.DocumentQuery`1.ToString()
   at Api.Data.Extensions.DocumentDBLinqExtensions.ChangeCase[T](IQueryable`1 query, QueryCase queryCase) in C:\Users\jason\source\repos\HeroPlatform\HeroPlatform\Api.Data\Extensions\DocumentDbLinqExtensions.cs:line 138

编辑:我发现从我在查询中使用的枚举顶部删除[JsonConverter(typeof(StringEnumConverter))]解决了错误(虽然现在我的数据试图按数字而不是字符串查询枚举)。

我实际上似乎没有在解决方案的任何项目中添加cosmos DB扩展版本。我正在创建一个IServiceCollection并为其提供以下DocumentClient:

new DocumentClient(
        new Uri(configuration["DocumentDb:EndpointUri"]),
        configuration["DocumentDb:Key"],
        serializerSettings: GlobalJson.SerializerSettings(),
        connectionPolicy: new ConnectionPolicy()
        {
            MaxConnectionLimit = 100,
            ConnectionMode = ConnectionMode.Direct,
            ConnectionProtocol = Protocol.Tcp,
        }))

GlobalJson.SerializerSettings()

var serializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCaseExceptDictionaryKeysResolver(),
                DateFormatHandling = DateFormatHandling.IsoDateFormat,
                DateParseHandling = DateParseHandling.DateTimeOffset,
                DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
            };
            serializerSettings.Converters.Add(new StringEnumConverter());

            return serializerSettings;
c# azure azure-functions azure-cosmosdb
1个回答
1
投票
© www.soinside.com 2019 - 2024. All rights reserved.