views:

441

answers:

3

Im trying to send a List of Items with generic Keys and Value Types to a asp.net webservice. How can i do this? I tried it with List<Dictionary<String, String>>, but this doens't work. I do not get an exception, but when i break in the webserivice, my collection is null. The Data could look like this: {"Data":{"fieldtype":"integer","isrequired":true,"isrequirederror":"Invalid Data"}}. But as i said, the Keys are generic, sometimes there are more properties, sometimes not.

Thx alot

This post couldn't help my btw: http://stackoverflow.com/questions/862271/how-to-send-a-hashtable-to-an-asmx-webservice

A: 
{"Data":
 {"fieldtype":"integer",
  "isrequired":true,
  "isrequirederror":"Invalid Data"
 }
}

: this is not a List<Dictionary<String, String>>
it's a Dictionary<string, Dictionary<String, String>>
could this help?

najmeddine
Now i get this exception: {"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.Dictionary`2[System.String,System.String]\u0027","StackTrace":" at System.Web.Script.Serialization
k0ni
+1  A: 

Classes that implement IDictionary can't be serialized to XML, it's a limitation of the XmlSerializer class. The usual workaround for that it to implement IXmlSerializable to provide custom serialization logic, as shown here. Another option is to transform your dictionary into a list of key/value pairs

Thomas Levesque
The solution with IXmlSerializable doesn't work for me, can you explain the other solution?
k0ni
what do you mean by "doesn't work" ? what exactly did you do ?
Thomas Levesque
How would i have to do it? I added the class and Updated my Wevservice Method.Anyhow...i think i have to convert all values to a string
k0ni
declare your object as a `SerializableDictionary<string, SerializableDictionary>` instead of `Dictionary<string, Dictionary>`
Thomas Levesque
i get this exception: Cannot convert object of type \u0027System.String\u0027 to type \u0027SerializableDictionary`2[System.String,System.Object]\u0027","StackTrace":"
k0ni
You don't give enough information to allow us to help you... show us some code !
Thomas Levesque
+1  A: 

Hello,

You are going to have hard times with SOA and WCF in particular if you want to introduce any kind of generics in your service - starting from serialization/deserialization issues and finishing interoperability issues.

Don't use generic parameters. Hashtable (or Dictionary) makes sense only within in-process usage due to read/write operations performance. If you want to transmit something in such format over the wire, use something like the following structure: List of pairs (string, string).

Vitaliy Liptchinsky