7

我遇到了一个问题,当我尝试使用 OfType() 方法对我的 odata 服务进行 linq 查询时,我收到一条错误消息,指出该请求对于版本 2 服务无效。我创建了 WCF 数据服务并将 MaxProtocolVersion 设置为 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;
    }
}

我的 edmx 有以下行:

<edmx:DataServices m:DataServiceVersion="1.0" 
    m:MaxDataServiceVersion="3.0" 
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

当我尝试执行以下查询时,我收到一条错误消息,指出“当 MaxProtocolVersion 小于 '3.0' 时,不支持方法 'OfType'。”

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();

如果我检查上下文的 MaxProtocolVersion,它设置为 v2。这在什么时候失败了?我该怎么做才能正确设置?

确切的错误:

{"MaxProtocolVersion 小于 '3.0' 时,不支持方法 'OfType'。"}

请求的堆栈跟踪:

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
4

2 回答 2

8

这是客户端的错误。必须使用 V3 构建 DataServiceContext 才能支持 V3 功能(如 OfType)。DataServiceContext 有一个新的构造函数重载,它采用 MaxProtocolVersion 参数。确保使用它并传入 V3。

于 2012-06-08T09:06:24.367 回答
4

除了如上所述安装 MSI 之外,您还可以通过扩展部分类来找到您正在寻找的 Nuget 构造器

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

这 3 行是直接从 Reference.cs 中复制的。

于 2013-02-04T22:13:50.733 回答