Send dictionary in Web service

Source: Internet
Author: User
Tags mscorlib wsdl

There is a need to pass dictionary<string, string> parameters in the Web service, for example:

typography shows pure text
[WebMethod]
Public dictionary<string> Process (dictionary<string> DCT) 
{
    Do something on the Dictionary
    ... blah blah blah ....
    return DCT;
}

God does not wish, the above-written method will produce Web service does not support IDictionary's bug:

The type System.Collections.Generic.Dictionary ' 2[[system.string, mscorlib, version=2.0.0.0, Culture=neutral, Publickeytoken=b77a5c561934e089],[system.string, mscorlib, version=2.0.0.0, Culture=neutral, PublicKeyToken= B77A5C561934E089]] is not supported because it implements IDictionary.

is not only the original sin of IDictionary, even if it is replaced with Hashtable, ListDictionary should also be the same result, test is really not a survivors free.

Google has discovered that there is a message from Ms Rd that this is a congenital limitation:

ASMX Web Services do not support types this implement IDictionary since V1. This are by design and was initially do since key-value constraints could not being appropriately expressed in schema.
Source: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/d7cb8844-6774-4a98-8aa3-85e445af4867/

Since it is by Design, it is only to walk in the way. Survey a little bit and found some suggestions:

    • Use XML instead
    • Using a DataSet
    • Another custom class as a reference
    • To dictionaryentry[] the melon generation

To evaluate, I originally wanted to rely on the simple structure of the dictionary key/value, XML for the open format is not easy to restrict the form of key/value; small demand for the dataset is slightly cumbersome; The custom class will make sure that the Key is , which is inconsistent with the problem of this case. See dictionaryentry[] to fit the demand, so I tried to write the following: (Just good dictionary and directionaryentry of the dual conversion are shown)

typography shows pure text
[WebMethod]
Public dictionaryentry[] Test (system.collections.dictionaryentry[] entries)
{
    With ListDictionary is mainly for a little later can be directly CopyTo dictionaryentry[]
    If you have efficiency or other considerations, you can use a different collection Class
    New ListDictionary ();
    In entries)
        Dct. Add (DE. Key, DE. Value);
    
    Do something on the Dictionary
    ... blah blah blah ....
    if (DCT. Contains ("Kuso"))
        dct["kill very much";
    
    New DICTIONARYENTRY[DCT. Count];
    Dct. CopyTo (result, 0);
    return result;
}

The call end example is as follows:

typography shows pure text
void Page_Load (object sender, EventArgs e)
{
    New localhost. Afawebservice ();
    Aws. Credentials = CredentialCache.DefaultCredentials;
    dictionary<new dictionary<string> (); 
    Dct. ADD ("You do not Go");
    DictionaryEntry is a custom category when Web service is transferred
    So use namespace. DictionaryEntry rather than System.Collections.DictionaryEntry
    New List<localhost. Dictionaryentry> ();
    foreach (inDCT. Keys)
    {
        New localhost. DictionaryEntry ();
        De. key = key;
        De. Value = Dct[key];
        Lst. Add (DE);
    }
    localhost. Dictionaryentry[] result = AWS. Test (LST. ToArray ());
    dictionary<new dictionary<string> (); 
    In result)
        Dctres.add (DE. Key.tostring (), DE. Value.tostring ());
    Response.Write (dct[","+ dctres["Kuso"]); 
    Response.End ();
}

It's not easy to see this way back to the fold totem.

So, I tried Paul Welter's Serializabledictionary object, reference this custom object on both the Web service and client side, and using Visual Studio's add Web Reference, the self-generated proxy class declares that Serializabledictionary will be lost as a dataset, so it has to be changed into a hand-generated proxy. After class, change the dataset back to Serializabledictionary:

c:\appcodefolder\>wsdl http://localhost/myweb/afawebservice.asmx? Wsdl/l:cs/n:localhost/out:afawebserviceproxy.cs
Microsoft (R) Web Services Description Language Utility
[Microsoft (R). NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file ' AfaWebServiceProxy.cs '.

After using Serializabledictionary, the code was simplified:

typography shows pure text
[WebMethod]
Public serializabledictionary<string> Test (
serializabledictionary<string> DCT)
{
    if (DCT. ContainsKey ("Kuso"))
        dct["kill very much";
    return DCT;
}

The call side is also very simple:

typography shows pure text
void Page_Load (object sender, EventArgs e)
{
    New localhost. Afawebservice ();
    Aws. Credentials = CredentialCache.DefaultCredentials;
    serializabledictionary<
             New serializabledictionary<string> ();
    Dct. ADD ("You do not Go");
    serializabledictionary<string> dctres = AWS. Test (DCT);
    Response.Write (dct[","+ dctres["Kuso"]); 
    Response.End ();
}

However, this practice requires adding a custom component to the Web service and client side, the Proxy class needs to be increased or modified by hand, or some inconvenience. This way, there are other gaps in the dataset or XML approach, but the built-in support is worth considering in a simple, easy-to-see fit.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.