WebAPI,oData和RavenDb - 抛出$ top和$ skip的错误

时间:2013-03-10 04:40:25

标签: asp.net-web-api ravendb odata

我正在使用WebAPI从RavenDb返回一系列基本的Supplier对象

供应商类别是:

public class Supplier
{
    public string Id { get; set; }
    public string Title { get; set; }
}

WebAPI方法是:

[Queryable]
public IQueryable<Supplier> Get()
{
    using (var session = _store.OpenSession())
    {
        return session.Query<Supplier>();
    }
}

当我致电http://localhost:8083/api/suppliers?$orderby=Id%20deschttp://localhost:8083/api/suppliers?$filter=Title%20eq%20'Test'时 一切正常。但是,每当我使用$ top或$ skip时,我都会遇到异常:

无法将“System.Linq.Expressions.PropertyExpression”类型的对象强制转换为“System.Linq.Expressions.ConstantExpression”。

如果我使用ToList()将整个集合返回到内存中,它可以正常工作,因此使用RavenDb延迟执行查询会出现问题。

我正在使用RavenDb.Client 2.0.2.2261的最新稳定版本。

是否还有其他人遇到此问题或有解决方法?

堆栈跟踪是:

   at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitQueryableMethodCall(MethodCallExpression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 994
   at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitMethodCall(MethodCallExpression expression, Boolean negated) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 693
   at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitExpression(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 140
   at Raven.Client.Linq.RavenQueryProviderProcessor`1.GetLuceneQueryFor(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 1318
   at Raven.Client.Linq.RavenQueryProviderProcessor`1.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:line 1352
   at Raven.Client.Linq.RavenQueryProvider`1.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProvider.cs:line 135
   at Raven.Client.Linq.RavenQueryProvider`1.System.Linq.IQueryProvider.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProvider.cs:line 190
   at Raven.Client.Linq.RavenQueryInspector`1.GetEnumerator() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryInspector.cs:line 99
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper(Object list)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.<WriteToStreamAsync>b__c()
   at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)

1 个答案:

答案 0 :(得分:9)

我能够重现你的问题,并成功地验证了我最初的猜测是什么可能出错。请尝试[Queryable(EnableConstantParameterization = false)]而不是普通[Queryable]。在OData开发周期的后期,我们进行了性能更改,以便最佳地使用EF automatic compilation of LINQ queries.我们默认启用它,因为它是一个安全的更改。不幸的是,我认为RavenDB LINQ提供程序无法理解通过此优化生成的LINQ查询。我共享的代码将其关闭并生成vanilla LINQ查询。