【问题标题】:Resource not found for segment 'Property'未找到段“属性”的资源
【发布时间】:2009-02-03 01:45:25
【问题描述】:

使用 ADO.Net 数据服务客户端通过调用 LoadProperty 刷新实体时:

ctx.BeginLoadProperty(this, "Owner", (IAsyncResult ar) => ...

如果属性是null,它会在服务器上抛出一个错误

错误:抛出异常:System.Data.Services.DataServiceException: 未找到分段“所有者”的资源。在 System.Data.Services.RequestDescription.GetSingleResultFromEnumerable(SegmentInfo 段信息)在 System.Data.Services.DataService1.CompareETagAndWriteResponse(RequestDescription 描述、ContentFormat responseFormat、IDataService dataService)
在 System.Data.Services.DataService1.SerializeResponseBody(RequestDescription 描述,IDataService 数据服务)在 System.Data.Services.DataService1.HandleNonBatchRequest(RequestDescription 描述)在 System.Data.Services.DataService`1.HandleRequest()

问题是客户端不知道该属性是null 还是尚未填充。属性 Owner 是从 VehicleCustomer 的链接。

有什么想法吗?

谢谢

【问题讨论】:

    标签: c# wcf-data-services


    【解决方案1】:

    将服务上下文的IgnoreResourceNotFoundException属性设置为true:

    svc.IgnoreResourceNotFoundException = true;
    

    【讨论】:

    • 请注意,此属性是在 .NET 3.5 SP1 中添加的。
    【解决方案2】:

    在主键不存在时查询主键会产生异常。解决方法是在条件中添加一个虚拟的 true 表达式(例如:1==1 && item.Id == XXX)。

    如果没有虚拟表达式,ADO.NET 请求是:

    http://localhost//test.svc/Role(XXX)

    使用虚拟条件,请求是:

    http://localhost//test.svc/Role()?$filter=true and (Id eq 1)

    在第二种情况下,预期的行为(返回 null)是正确的。

    【讨论】:

    • 这个other answer 是一个更好的选择。使代码更具可读性。
    【解决方案3】:

    我还收到了“未找到段'属性'的资源”错误。我的似乎是在我按主键查找的 where 子句中。我发现一些资源说它会在使用不存在的主键时抛出错误(即使使用 FirstOrDefault())(与其他不提供结果的 where 子句相反)。我猜类似的事情也发生在你自己身上。

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2015-11-30
      • 2021-10-19
      • 2014-04-22
      • 2013-11-28
      • 1970-01-01
      相关资源
      最近更新 更多