Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations

WCF Serialization Part 2c: Hacking the NameValueCollection (unsuccesfully)

As we mentioned here and here, I've been struggling to get the NameValueCollection object to pass through WCF serialization. Please read the first two posts first for some context.


In the previous episodes, we saw that we can't get WCF to serialize the NameValueCollection (NVC, from now on) because it incorrectly marked for its CollectionDataContract serialization, but then choked because it had no Add(object) method.


So to take the most direct approach, I subclassed the NameValueCollection and added my own Add(object) implementation to see if this jedi mind trick will let WCF work with the NVC class:


public class NVC : NameValueCollection


    public void Add(object obj)





Once I did this, the WCF stopped yelling at me that the contract was invalid, but (naturally) my data would vanish during transfer.


So next I tried putting a breakpoint inside my Add() method to see what object is passed to it – I was hoping for something along the lines of a KeyValuePair<K,V> or even a DictionaryEntry, something I can use to manually recreate my NVC.

Turns out there's no such luck – even though the NVC has an internal object called NameObjectEntry that the NVC uses internally for storage, it's not exposed externally. Even the GetEnumerator() method returns an enumerator that only goes over the Keys, not a key/value dictionary.

This means that when recreating my dictionary, the value passed to my Add() method is a single string with the key name only, and the value is lost in translation.


No luck there, either.

Posted: Aug 02 2006, 04:28 PM by AvnerK | with 5 comment(s)
Filed under: ,


AndrewSeven said:

Can you change to use IDictionary<string,string> instead of the NVC?

# August 2, 2006 1:02 PM

AvnerK said:

As I mentioned in the first part, I can easily use a Dictionary<string, string> over WCF, but that doesn't solve my serialization problem when persisting it to disk later.

# August 2, 2006 6:05 PM

Wink Jr. said:

Thank you for saving me who knows how many hours trying to get KeyValuePair working with this, which you think would be a no-brainer, obvious, and built-in.  Cheers!

# February 13, 2008 2:37 AM

Rob V. said:

This might be fixed in VS 2008 (v9.0) with C# 3.0 and .NET 3.5.  I haven't tested the code, but the IDE is not throwing up errors with the following:


KeyValuePair<String, String> nvp = KeyValue<String, String>;

foreach (KeyValuePair<String,String> nvp in NameValueCollection nvc) {


   string key = nvp.Key;

   string value = nvp.Value;



Note <String, String>, <b>NOT</b> <string, string>.  Needs to be the object type, not the inherent type.

Anyway, not sure if this will work, but as said, VS 2008 is not complaining w/C# 3.0 and .NET 3.5.

# February 13, 2008 2:46 AM

Billy said:

I don't think there are any differences between <string, string> and <String, String>, as "string" is simply the alias of System.String.

# September 23, 2008 3:24 AM

Layton said:

I just put that the link of your blog on my Facebook Wall.

very nice blog indeed.`-*~;

# January 12, 2013 4:20 PM