DataServiceVersion conflict: The DataServiceVersion '1.0' is too low for the request. The lowest supported version is '3.0'

落爺英雄遲暮 提交于 2019-12-25 01:45:31

问题


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.

The DataServiceVersion '1.0' is too low for the request. The lowest supported version is '3.0'

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

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

Query that works.

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

The URL that query generates.

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

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.

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.

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..

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

The DataServiceVersion '2.0' is too low for the request. The lowest supported version is '3.0'. See exception below for more details.

which seems to back up my hypothesis..

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?


回答1:


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");
    };

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.



来源:https://stackoverflow.com/questions/17842410/dataserviceversion-conflict-the-dataserviceversion-1-0-is-too-low-for-the-req

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!