Search code examples
web-servicesidictionary

Passing IDictionary object to Web service


In my asmx file, I have

        [WebMethod]
        [ScriptMethod]
        public void Method(IDictionary<string, CustomClass> objectOfCustomClass)
        {
           //do stuff
        }

custom class is defined as:

public class CustomClass
    {
        public string Prop1 { get; set; }
        public string Prop2 { get; set; }       
    }

All goes well and the web service is being successfully consumed from Jquery ajax method. Recently I decided to test and try and access it from the direct URL such as http://localhost/Services.asmx/Method

and I get this message

"Cannot serialize interface System.Collections.Generic.IDictionary"

This seems off. What's causing it and is it normal? I am confused - it seems like the clean way of doing things but according to Microsoft shouldn't be done, yet it works, only not when accessing the web service directly. What gives? I also read on some MS sites that you can't pass IDictionary to the web service as a parameter yet it works fine...SO can it be done or not? Can someone clarify this once and for all?


Solution

  • Unfortunately, yes, this is normal. What you see is a limitation of XML serialization (the XmlSerializer) in the classic ASP.NET Web Service stack - it refuses to work with anything that implements IDictionary.

    The service exposed to JavaScript via [ScriptMethod] uses a different serializer (the JavaScriptSerializer), which does not have this limitation.

    When you call the service from JavaScript, you invoke the JSON endpoint (declared with [ScriptMethod]). When you test from the browser, however, you reach the traditional XML enpoint (declared with [WebMethod]).

    There are some workarounds, see e.g. this question. However, if you only need to support AJAX clients, you may simply remove the XML endpoint (the [WebMethod] attribute) and avoid the issue.

    As a side note, the improved serializer in WCF supports serialization of dictionaries.