I'm having a problem that when I attempt to do a linq query against my odata service with the OfType() method, I get an error saying that the request isn't valid for a Version 2 service. I have created the WCF Data Service and have set the MaxProtocolVersion to v3.
public class TestDirectorySearch : DataService<TestDirectoryEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetEntitySetPageSize("*", 50);
config.UseVerboseErrors = true;
config.DataServiceBehavior.MaxProtocolVersion = System.Data.Services.Common.DataServiceProtocolVersion.V3;
}
}
My edmx has the following line:
<edmx:DataServices m:DataServiceVersion="1.0"
m:MaxDataServiceVersion="3.0"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
When I attempt to do the following query I get an error saying that the "The method 'OfType' is not supported when MaxProtocolVersion is less than '3.0'."
from test in context.Tests.OfType<OrderableTest>()
where test.TestRevisionId.Equals(mmt.RevisionId)
select new
{
Reflex = test.ReflexTest
, ShipTemp = (test.SpecimenTemperature == null) ? null : test.SpecimenTemperature.DisplayDescription
}).FirstOrDefault();
If I check the MaxProtocolVersion of my context, it is set at v2. At what point is this failing? What can I do to set this correctly?
Exact error:
{"The method 'OfType' is not supported when MaxProtocolVersion is less than '3.0'."}
Requested stack trace:
at System.Data.Services.Client.ResourceBinder.AnalyzeOfType(MethodCallExpression mce, DataServiceProtocolVersion maxProtocolVersion)
at System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce)
at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
at System.Data.Services.Client.ALinqExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original)
at System.Data.Services.Client.ALinqExpressionVisitor.VisitMethodCall(MethodCallExpression m)
at System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce)
at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
at System.Data.Services.Client.ResourceBinder.AnalyzeProjection(MethodCallExpression mce, SequenceMethod sequenceMethod, Expression& e)
at System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce)
at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
at System.Data.Services.Client.ALinqExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original)
at System.Data.Services.Client.ALinqExpressionVisitor.VisitMethodCall(MethodCallExpression m)
at System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce)
at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
at System.Data.Services.Client.ResourceBinder.Bind(Expression e, DataServiceContext context)
at System.Data.Services.Client.DataServiceQueryProvider.Translate(Expression e)
at System.Data.Services.Client.DataServiceQuery`1.Translate()
at System.Data.Services.Client.DataServiceQuery`1.Execute()
at System.Data.Services.Client.DataServiceQuery`1.GetEnumerator()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at System.Data.Services.Client.DataServiceQueryProvider.ReturnSingleton[TElement](Expression expression)
at System.Data.Services.Client.DataServiceQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
at TDMixBLL.TestDirectoryServiceHandler.PopulateOrderableTestInfo(MonitoredMixTest mmt) in C:\Dev\TDMix\ANSR - TDMix\TDMix2\TDMixBLL\TDMixBLL\TestDirectoryServiceHandler.cs:line 161
at TDMixBLL.TestDirectoryServiceHandler.PopulateTestInfo(MonitoredMixTest test) in C:\Dev\TDMix\ANSR - TDMix\TDMix2\TDMixBLL\TDMixBLL\TestDirectoryServiceHandler.cs:line 124
at TDMixBLL.TestDirectoryServiceHandler.GetTest(Int64 testRevisionId) in C:\Dev\TDMix\ANSR - TDMix\TDMix2\TDMixBLL\TDMixBLL\TestDirectoryServiceHandler.cs:line 112
at TDMixBLL.TestDirectoryServiceHandler.PopulateTests(List`1 testsToPopulate) in C:\Dev\TDMix\ANSR - TDMix\TDMix2\TDMixBLL\TDMixBLL\TestDirectoryServiceHandler.cs:line 66
at TDMix2.Tests.TestRetrievalTests.TestPopulate() in C:\Dev\TDMix\ANSR - TDMix\TDMix2\TDMix2.Tests\TestRetrievalTests.cs:line 38
This is an error on the client. The DataServiceContext has to be constructed with V3 in order to support V3 features (like OfType). DataServiceContext has a new constructor overload which takes the MaxProtocolVersion parameter. Make sure that you use it and pass in V3.
In addition to installing the MSI as above, you can find your way into the Nuget constructur you're looking for by extending the partial class
namespace Project.ServiceReference
{
public partial class MyEntities : global::System.Data.Services.Client.DataServiceContext
{
public TestDirectoryEntities(string uri)
: base(new Uri(uri), DataServiceProtocolVersion.V3)
{
this.ResolveName = new global::System.Func<global::System.Type, string>(this.ResolveNameFromType);
this.ResolveType = new global::System.Func<string, global::System.Type>(this.ResolveTypeFromName);
this.OnContextCreated();
}
}
}
The 3 lines are copied directly from the Reference.cs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With