views:

1179

answers:

2

Using ASP.NET WebServices, by default you can't pass any IDictionary objects, because xml serialization for IDictionaries isn't supported. I have found various ways to serialized dictionaries (http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx, http://blogs.msdn.com/psheill/archive/2005/04/09/406823.aspx, http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/) but, I can't find any way to hook this into the Asp.NET web methods.

Is there a way to hook it up so that I can use IDictionaries with WebMethods?

A: 

depends on how you are calling these methods, we use the .net 2.0 method with "web references" and you will notice that the WSDL generated class is a partial, we create a (web reference name).cs file and have something like:


public partial class WebReferenceName : System.Web.Services.Protocols.SoapHttpClientProtocol 
{
    // Overload the WSDL generated method that only takes a simple array
    public ReturnType MethodName(List  collection)
    {
     // redirect the call to the wsdl generated method with the appropriate parameters
     return this.MethodName(collection.ToArray());
    }
}

Overloading will allow you to call the method name with either type of collection

At the web service side you can convert from a simple collection back to a more powerful collection, most likely with the use of that more powerful collections ctor. One of the ctors will probably consume a simple collection.

Allen
A: 

The question is - where do you want to pass the dictionary? If you simply want to teach your service how to serialize a dictionary, then implement the IXmlSerializable interface on your dictionary class, and serialize it however you like.

The problem will be on the client. How should a Java client deserialize what you sent? For that matter, how would a .NET client know how to do that? There's no good way when using the old ASMX web services to have the client do something special. It's one of many reasons that WCF was invented.

John Saunders