tags:

views:

21

answers:

2

I currently have an asmx method defined like so :

[WebMethod]
public String Method1(Hashtable form)

It receives json objects with a variable number of attributes, for example :

{"form":{"name1":"10","name2":"20"}}

This is working fine and gives the expected results when called, but when I open the web service address in the browser, I get the error :

The type System.Collections.Hashtable is not supported because it implements IDictionary

I've tried other data types, like List<DictionaryEntry> which will fix this, but then be empty when the method is called, and I can't find something that will work in both cases...

What is the "correct" way of doing this?

+1  A: 

IDictionary cannot be serialized to XML (which is how asmx web services work), so you cannot use any implementation of IDictionary either as a return value or as a parameter.

So the "correct" way of doing this is to use anything that doesn't implement IDictionary. You could do something like this instead:

[WebMethod]
public String Method1(params KeyValuePair<string, string>[] formdata)

and then call it like this:

service.Method1(new KeyValuePair("name1", "10"), new KeyValuePair("name2", "20"));
MusiGenesis
I tried this method definition, but I get a "No parameterless constructor defined for the type of KeyValuePair<String, String>[]" error. I should also mention that I don't have control over the incoming json data.
tjsar
A: 

For the time being, I can do this as a workaround :

[WebMethod]
public String Method1(Object form)
{
    Dictionary<String, Object> data = (Dictionary<String, Object>)form;

And the service .asmx page loads without error.

This still makes no sense to me though...

tjsar