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

时间:2019-04-23 15:45:21

标签: c# azure azure-functions azure-cosmosdb

我有一个Azure函数应用程序,其中运行有多个函数。如果多个查询最终在同一时间运行,那么我将从DocumentQuery代码的深处得到一个InvalidCastException。如果我仅禁用一个功能(与哪个功能无关),就不会发生该错误,这使我相信存在某种共享内存问题。

我已删除所有内容,但有问题的查询除外,以试图隔离问题,从而确认问题确实源于对波斯菊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;

1 个答案:

答案 0 :(得分:1)