Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF serialization problem using NameValueCollection

I'm trying to serialize a NameValueCollection over WCF. I keep getting exceptions telling me to add one type after another. After adding them, I finally get

Type 'System.Object[]' cannot be added to list of known types since another type 'System.Collections.ArrayList' with the same data contract name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfanyType' is already present.

The contract now looks like this:

[KnownType(typeof(NameValueCollection))]
[KnownType(typeof(CaseInsensitiveHashCodeProvider))]
[KnownType(typeof(CaseInsensitiveComparer))]
[KnownType(typeof(string[]))]
[KnownType(typeof(Object[]))]
[KnownType(typeof(ArrayList))]
[DataContract]
public class MyClassDataBase
{
    [DataMember]
    public NameValueCollection DataCollection = new NameValueCollection();
}

I really dont know what to do to be able to serialize my NameValueColletion.

Another strange thing is that the compiler warns that the CaseInsensitiveHashCodeProvider is deprecated.

like image 347
kaze Avatar asked Jun 02 '09 11:06

kaze


2 Answers

The best idea would be to stop using weak types like NameValueCollection and ArrayList. Use Dictionary<string,string> and List<T> instead.

like image 186
John Saunders Avatar answered Oct 04 '22 22:10

John Saunders


The NameValueCollection doesn't seem exactly to be designed for this use case. There is a series of blog posts on this blog that deals with this problem and presents a possible solution (to wit: use an IDictionary). I haven't tested it though.

like image 45
dtb Avatar answered Oct 04 '22 21:10

dtb