DataServiceVersion冲突:DataServiceVersion'1.0'对于请求而言太低.支持的最低版本是"3.0" [英] DataServiceVersion conflict: The DataServiceVersion '1.0' is too low for the request. The lowest supported version is '3.0'

查看:106
本文介绍了DataServiceVersion冲突:DataServiceVersion'1.0'对于请求而言太低.支持的最低版本是"3.0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此技术(

Using this technique ( How to dynamic add filters to a LINQ query against an Odata Source in C# ) I dynamically build up a where clause in a LINQ query to my Odata endpoint. I do this often and it works great. However now my query contains some odata v3.0 features and i'm getting this error.

对于请求,DataServiceVersion'1.0'太低.支持的最低版本是"3.0"

我已经使用唯一的Northwind端点构建了一个简单的复制,并且可以用标准C#代码或linqpad对其进行复制.

I've build a simple reproduction using the only northwind endpoint at , and can reproduce it in standard C# code or in linqpad.

Northwind端点: http://services.odata.org/Northwind/Northwind.svc /

Northwind Endpoint: http://services.odata.org/Northwind/Northwind.svc/

有效的查询.

from x in Categories
where x.Products.Any( e => e.ProductName == "chai")
select x

查询生成的URL.

http://services.odata.org/Northwind/Northwind.svc/Categories()?$filter=Products/any(e:e/ProductName eq 'chai')

这是查询,如果我已经动态生成了where子句(就像我在上面链接的技术中所做的那样)

and here is the query, if i had generated the where clause dynamically (as i do in the technique linked above)

 from x in Categories.AddQueryOption("$filter",  "Products/any(e:e/ProductName eq 'chai')")
 select x

但是现在我得到了错误.

However now i get the error.

当我查看DataServiceContext时,它有一个MaxProtocolVersion,上面显示的是V3,当然我的项目是dotnet 4.5,普通查询正在工作.

When i look at my DataServiceContext it has a MaxProtocolVersion that says V3 and of course my project is dotnet 4.5 and the plain query is working.

所以我认为正在发生..服务器是否支持V3功能,并且我正在使用它,但是客户端必须查看LINQ查询并将其DataServiceVersion标识为该功能中使用的功能的最低公分母.查询,并且因为我使用的是V3功能,但仅在它无法推理的动态部分,它认为我仅使用了V1功能.

So what I think is happening.. is the server supports V3 features, and i'm using it, but the client must look at the LINQ query and identify its DataServiceVersion as the lowest common denominator of features that are used in the query, and because i'm using V3 feature, but only in the dynamic part that it can't reason about, it thinks i'm only using V1 features..

如果我在下面的选择中使用了V2功能(投影)

If i use a V2 feature (projection) in the select as below

from x in Categories.AddQueryOption("$filter",  "Products/any(e:e/ProductName eq 'chai')")
 select new { x.CategoryID, x.CategoryName }

然后我得到一个略有不同的错误

then i get a slightly different error

对于请求,DataServiceVersion'2.0'太低.支持的最低版本是"3.0".有关更多详细信息,请参见下面的异常.

这似乎支持了我的假设.

which seems to back up my hypothesis..

所以我可以看到2个解决方案 1)以某种方式告诉DataServiceContext或引擎强制客户端将查询标识为V3,或者像上面我在选择查询时使用V2功能,从而调出引擎可以解决的最低公分母"从查询的一部分可以识别出来,所以同样,我可以在Select或其他子句中使用某些V3功能来强制查询实际上是V3吗?

so i can see 2 solutions 1) someway to tell the DataServiceContext or the engine to FORCE the client to identify the query as V3, or like above i used a V2 feature in the select of the query, thus bringing up the "lowest common denominator" the engine can work out from the part of the query it can identify, so likewise is there some V3 feature i could use in the Select, or another clause just to force the query to actually be V3?

有人知道有什么可以帮助的吗?

does anybody know anything that could help?

推荐答案

这是我发现有效的方法.基本上,我只是钩住并覆盖了HTTP标头

This is what i found worked. Basically I just am hooking and overwriting the HTTP Header

MyDatacontext.SendingRequest2 += (sender, eventArgs) => {                 
           eventArgs.RequestMessage.SetHeader("DataServiceVersion", "3.0;NetFx");
    };

我使用的是WCF 5.0,因此使用的是SendingRequest2,但是,如果使用的是旧版本,则可以使用已贬值的SendingRequest或BuildingRequest事件.

I am using WCF 5.0 so are using the SendingRequest2 , however if using an older version you could use likely use the depreciated SendingRequest or BuildingRequest events instead.

这篇关于DataServiceVersion冲突:DataServiceVersion'1.0'对于请求而言太低.支持的最低版本是"3.0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆