如何序列化 Dictionary<string, string>通过 WCF? [英] How to serialize Dictionary&lt;string, string&gt; through WCF?

查看:53
本文介绍了如何序列化 Dictionary<string, string>通过 WCF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据合同,其中的数据成员类型为 Dictionary.

I have a data contract with a data member typed as Dictionary<string, string>.

生成的 Web 服务引用将其公开为类型为 ArrayOfKeyValueOfstringKeyValueOfstringstring[] 的成员.

The generated web service reference exposes this as a member with the type ArrayOfKeyValueOfstringstringKeyValueOfstringstring[].

有人见过这个吗?

推荐答案

WCF 仅序列化结构,因为最终在线路上的内容必须足够独立,以便对任何人有用 客户端,而不仅仅是 .NET 客户端.

WCF only serializes structure, because what ends up on the wire must be self-contained enough to be useful for any client, not just .NET clients.

某些在不同平台上开发的客户端可能不共享字典"的概念,因此将字典序列化为携带有关底层类的隐式知识的表示会过于受限.

Some client developed on a different platform may not share the concept of a 'dictionary', so it would be too constraining to serialize a Dictionary to a representation that carries an implicit knowledge about the underlying class.

客户端甚至可能不是面向对象的.

The client may not even be object-oriented.

字典不仅仅是结构 - 它还包含行为(例如,当您分配给一个已经存在的键时,您会覆盖该键等)并且该行为不能通过线路传播.

A Dictionary is more than structure - it also contains behavior (e.g. when you assign to a key that already exists, you overwrite that key, etc.) and that behavior cannot travel across the wire.

换句话说,字典和许多其他 .NET 类型是不可互操作的,因此 WCF 不会尝试将它们保存在 ServiceContract 中.

In other words, Dictionaries and many other .NET types are not interoperable, so WCF will not attempt to preserve them in a ServiceContract.

您最好为您的数据设计一个自定义的DataContract.

You would probably be better off designing a custom DataContract for your data.

这篇关于如何序列化 Dictionary<string, string>通过 WCF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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