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.
The best idea would be to stop using weak types like NameValueCollection
and ArrayList
. Use Dictionary<string,string>
and List<T>
instead.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With