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.

Published Wednesday, August 02, 2006 4:28 PM by AvnerK

Filed under: ,

Comments

# re: WCF Serialization Part 2c: Hacking the NameValueCollection (unsuccesfully)@ Wednesday, August 02, 2006 1:02 PM

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

AndrewSeven

# re: WCF Serialization Part 2c: Hacking the NameValueCollection (unsuccesfully)@ Wednesday, August 02, 2006 6:05 PM

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.

AvnerK

# re: WCF Serialization Part 2c: Hacking the NameValueCollection (unsuccesfully)@ Wednesday, February 13, 2008 2:37 AM

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!

Wink Jr.

# re: WCF Serialization Part 2c: Hacking the NameValueCollection (unsuccesfully)@ Wednesday, February 13, 2008 2:46 AM

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:

<pre><code>

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

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

   [....]

   string key = nvp.Key;

   string value = nvp.Value;

}

</code></pre>

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.

Rob V.

# re: WCF Serialization Part 2c: Hacking the NameValueCollection (unsuccesfully)@ Tuesday, September 23, 2008 3:24 AM

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

Billy

Leave a Comment

(required) 
(required) 
(optional)
(required)