条件WCF DataContract序列化 [英] Conditional WCF DataContract Serialization

查看:67
本文介绍了条件WCF DataContract序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的WCF服务上实现一个条件DataContract序列化,就像在这个代码项目示例中一样:  https://www.codeproject.com/Articles/417168/Conditional-WCF preferencesContract-Serialization-使用

I would like to implement a conditional DataContract serialization on my WCF service, like in this codeproject example: https://www.codeproject.com/Articles/417168/Conditional-WCF-DataContract-Serialization-Using

我想实现的目标是我可以在服务上拥有静态DataContract对象,当客户端请求此对象时,只有一些DataMembers返回给客户端以减少邮件大小。然后,客户端可以单独请求这些
子对象。此外,服务器应该能够序列化整个DataContract,包括所有DataMembers。

The goal I would like to achieve is that I can have static DataContract object on the service and when the client requests this object only some of the DataMembers are returned to the client to reduce the message size. The client could then request those sub object individually. Also the server should be able to serialize the whole DataContract including all DataMembers.

问题是codeproject示例更改了源对象的实际属性,因此我必须克隆整个发送前的对象,以便我的静态DataContract不会更改。最好的解决方案是,如果我可以创建一个自定义DataMember
属性,该属性只能使用自定义DataContractSerializer进行序列化,同时也会查找此属性。

The problem is that the codeproject example changes the actual property of the source object, so I would have to clone the whole object before sending so that my static DataContract is not changed. The best solution would be if I could create a custom DataMember attribute that would only be serialized with a custom DataContractSerializer that also looks for this attribute.

我发现的唯一解决方案远程检查实际属性中的StackTrace并检查它是否是从"保存"中调用的。在我的服务上的方法,然后我属性将返回它的值。但是我不知道这是否会为这个问题提供一个很好的解决方案。

The only solution I found so far would be to check the StackTrace inside the actual properties and check if it was called from the "Save" method on my service and only then I the property would return it's value. But I don't know if this would be a good solution for this problem.

有更好的方法吗?

推荐答案

嗨TheNetStriker,你好b $ b根据你的描述,我认为你的想法很棒。 虽然我有一些问题,但这些DataMember属性是否有助于实现您的最终目标?

Hi TheNetStriker,
According to your description, I think your idea is great.  While I have some questions, are these DataMember properties useful to achieve your final goal?
[DataMember(IsRequired = false)]
[IgnoreDataMemeber]
[DataMember(EmitDefaultValue = false)]




为什么客户需要单独请求这些子类?如果个别成员被明确标记为非必需成员,那么在服务器和客户端之间传递这些非必需成员(可以按需忽略)将不会出错。

最好的问候

亚伯拉罕


Why do clients need to request these subclasses separately? If individual members are explicitly marked as non-required members, passing these non-required members (which can be ignored on demand) between the server and the client will not go wrong.
Best Regards
Abraham


这篇关于条件WCF DataContract序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆