Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

serialization - How to serialize Dictionary<string, string> through WCF?

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

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

Has anyone seen this before?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...