对 List进行子类化时,WCF 返回数据丢失数据

发布于 2024-10-21 14:24:30 字数 1703 浏览 10 评论 0原文

我正在客户端和服务器之间共享合同(不使用生成的代理)。这是合同(注意我也尝试过 ObservableCollection):

public class MyList : List<MyItem> {
    public int Total { get; set; }
    public MyList() { }
}

当在服务器上调用 EndMyOperation 异步回调方法时,仍然设置 Total。但是,当我查看客户端上的值时,总计消失了(设置为 0)。跟踪清楚地表明附加属性没有被传递。谁能解释为什么会发生这种情况?

<MessageLogTraceRecord>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/MyOperation</Action>
    <ActivityId CorrelationId="d2872ac2-685e-4f94-b516-aaba4effa463" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">695d611f-2b3d-48ca-80b8-7ceae4fd423f</ActivityId>
    </s:Header>
    <s:Body>
      <MyOperationResponse xmlns="http://tempuri.org/">
        <MyOperationResult xmlns:d4p1="http://schemas.datacontract.org/2004/07/MyDataContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <d4p1:MyItem>
            <d4p1:MyId>1</d4p1:MyId>
          </d4p1:MyItem>
          <d4p1:MyItem>
            <d4p1:MyId>12</d4p1:MyId>
          </d4p1:MyItem>
        </MyOperationResult>
      </MyOperationResponse>
    </s:Body>
  </s:Envelope>
</MessageLogTraceRecord>

只是为了澄清,我知道我可以解决这个问题,通过不子类化并执行如下所示的操作。相反,我试图理解为什么会发生上述行为。

public class MyPagedList<T> { 
    public int Total { get; set; } 
    public List<T> MyList { get; set; }
    public MyPagedList() { MyList = new List<T>(); }
}

I am sharing contract between my client and server (not using generated proxies). Here is the contract (note I have also tried ObservableCollection):

public class MyList : List<MyItem> {
    public int Total { get; set; }
    public MyList() { }
}

The Total is still set when the EndMyOperation async callback method is called on the server. However, when I look at the value on the client the Total is gone (set to 0). The trace clearly shows that the additional attribute is not being passed. Can anyone shed some light on why this is happening?

<MessageLogTraceRecord>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/MyOperation</Action>
    <ActivityId CorrelationId="d2872ac2-685e-4f94-b516-aaba4effa463" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">695d611f-2b3d-48ca-80b8-7ceae4fd423f</ActivityId>
    </s:Header>
    <s:Body>
      <MyOperationResponse xmlns="http://tempuri.org/">
        <MyOperationResult xmlns:d4p1="http://schemas.datacontract.org/2004/07/MyDataContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <d4p1:MyItem>
            <d4p1:MyId>1</d4p1:MyId>
          </d4p1:MyItem>
          <d4p1:MyItem>
            <d4p1:MyId>12</d4p1:MyId>
          </d4p1:MyItem>
        </MyOperationResult>
      </MyOperationResponse>
    </s:Body>
  </s:Envelope>
</MessageLogTraceRecord>

Just to clarify I know I can solve the issue, by not subclassing and doing something like below. Rather I am trying to understand why the above behavior happens.

public class MyPagedList<T> { 
    public int Total { get; set; } 
    public List<T> MyList { get; set; }
    public MyPagedList() { MyList = new List<T>(); }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2024-10-28 14:24:30

MyItem [Serializable] 吗?或者更确切地说,MyItem 上的字段是可序列化的吗?

或者这可能是您的问题:inheriting-generic-collection-and-serialization

Is MyItem [Serializable]? Or rather are the fields on MyItem Serializable?

Or could this be your problem: inheriting-generic-collection-and-serialization

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文