对 List进行子类化时,WCF 返回数据丢失数据
我正在客户端和服务器之间共享合同(不使用生成的代理)。这是合同(注意我也尝试过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是
MyItem
[Serializable]
吗?或者更确切地说,MyItem
上的字段是可序列化的吗?或者这可能是您的问题:inheriting-generic-collection-and-serialization
Is
MyItem
[Serializable]
? Or rather are the fields onMyItem
Serializable?Or could this be your problem: inheriting-generic-collection-and-serialization