有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如:

排版顯示純文字
[WebMethod]
public Dictionary<string, string> Process(Dictionary<string, string> dct)
{
    //Do something on the Dictionary
    //... blah blah blah ....
 
    return dct;
}

天不從人願,以上的寫法會產生Web Service不支援IDictionary的錯誤:

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.

既是IDictionary的原罪,就算是換用Hashtable、ListDictionary應該也是同樣結果,測試之下果然無一倖免。

Google發現討論區有篇來自MS RD的留言,算是證實這是先天限制:

ASMX web services do not support types that implement IDictionary since V1. This is by design and was initially done since key-value constraints could not be appropriately expressed in schema.
來源: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/d7cb8844-6774-4a98-8aa3-85e445af4867/

既然是By Design,就只有繞道而行。Survey了一下,找到一些建議做法:

  • 改用XML
  • 使用DataSet
  • 另外自訂Class作為參數
  • 以DictionaryEntry[]瓜代之

評估了一下,我原本想要借重的就是Dictionary Key/Value的單純資料結構,XML為開放格式不易限制成Key/Value的形式;小小需求動用到DataSet略嫌笨重;自訂Class在編譯時期就要確定Key的種類,不符本案例的前題。看來DictionaryEntry[]較合需求,因此我試寫如下: (剛好Dictionary與DirectionaryEntry的雙向轉換都有示範到)

排版顯示純文字
[WebMethod]
public DictionaryEntry[] Test(System.Collections.DictionaryEntry[] entries)
{
    //用ListDictionary主要是為了稍後可以直接CopyTo轉DictionaryEntry[]
    //若有效率或其他考量,可改用其他Collection Class
    ListDictionary dct = new ListDictionary();
    foreach (DictionaryEntry de in entries)
        dct.Add(de.Key, de.Value);
    
    //Do something on the Dictionary
    //... blah blah blah ....
    if (dct.Contains("Kuso"))
        dct["Kuso"] = "殺很大";
    
    DictionaryEntry[] result = new DictionaryEntry[dct.Count];
    dct.CopyTo(result, 0);
    return result;
}

呼叫端範例如下:

排版顯示純文字
protected void Page_Load(object sender, EventArgs e)
{
    localhost.AFAWebService aws = new localhost.AFAWebService();
    aws.Credentials = CredentialCache.DefaultCredentials;
    Dictionary<string, string> dct = new Dictionary<string, string>();
    dct.Add("Kuso", "你不要走");
    //DictionaryEntry在Web Service傳遞時會被當成自訂類別
    //因此要用namespace.DictionaryEntry而非System.Collections.DictionaryEntry
    List<localhost.DictionaryEntry> lst = new List<localhost.DictionaryEntry>();
    foreach (string key in dct.Keys)
    {
        localhost.DictionaryEntry de = new localhost.DictionaryEntry();
        de.Key = key;
        de.Value = dct[key];
        lst.Add(de);
    }
    localhost.DictionaryEntry[] result = aws.Test(lst.ToArray());
    Dictionary<string, string> dctRes = new Dictionary<string, string>();
    foreach (localhost.DictionaryEntry de in result)
        dctRes.Add(de.Key.ToString(), de.Value.ToString());
    Response.Write(dct["Kuso"] + "->" + dctRes["Kuso"]);
    Response.End();
}

經過這番來回折騰,這方法看來也不怎麼簡潔。

於是,我又嘗試了Paul Welter的SerializableDictionary物件,做法上要在Web Service與Client端都Reference這個自訂物件,而且使用Visual Studio的Add Web Reference時,自動產生的Proxy Class宣告中SerializableDictionary會被當成DataSet而失敗,因此得改成手動產生Proxy Class後再將DataSet改回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'.

用了SerializableDictionary後,程式碼簡化許多:

排版顯示純文字
[WebMethod]
public SerializableDictionary<string, string> Test(
 SerializableDictionary<string, string> dct)
{
    if (dct.ContainsKey("Kuso"))
        dct["Kuso"] = "殺很大";
    return dct;
}

呼叫端也很單純:

排版顯示純文字
protected void Page_Load(object sender, EventArgs e)
{
    localhost.AFAWebService aws = new localhost.AFAWebService();
    aws.Credentials = CredentialCache.DefaultCredentials;
    SerializableDictionary<string, string> dct = 
             new SerializableDictionary<string, string>();
    dct.Add("Kuso", "你不要走");
    SerializableDictionary<string, string> dctRes = aws.Test(dct);
    Response.Write(dct["Kuso"] + "->" + dctRes["Kuso"]);
    Response.End();
}

但是,這個做法需要在Web Service與Client端加入自訂元件參照、Proxy Class需要手動增加或修改,還是有些許不便。這樣看來,DataSet或XML法雖有其他缺點,但內建支援的特點,在力求簡單的場合裡,倒也值得納入考量吧!

在Web Service中傳送Dictionary的更多相关文章

  1. 问题:不支持Dictionary;结果:在Web Service中傳送Dictionary

    在Web Service中傳送Dictionary 有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMe ...

  2. Web Service 中返回DataSet结果大小改进

    http://www.cnblogs.com/scottckt/archive/2012/11/10/2764496.html Web Service 中返回DataSet结果方法: 1)直接返回Da ...

  3. Web Service 中返回DataSet结果的几种方法

    Web Service 中返回DataSet结果的几种方法: 1)直接返回DataSet对象    特点:通常组件化的处理机制,不加任何修饰及处理:    优点:代码精减.易于处理,小数据量处理较快: ...

  4. ASP.NET Web Service中使用Session 及 Session丢失解决方法 续

    原文:ASP.NET Web Service中使用Session 及 Session丢失解决方法 续 1.关于Session丢失问题的说明汇总,参考这里 2.在Web Servcie中使用Sessio ...

  5. 转-Web Service中三种发送接受协议SOAP、http get、http post

    原文链接:web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 一.web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 在web服务中,有三种可供选择的发 ...

  6. 企业级SOA之路——在Web Service中使用HTTP和JMS

    原文:http://www.tibco.com/resources/solutions/soa/enterprise_class_soa_wp.pdf   概述     IT业界在早期有一种误解,认为 ...

  7. Web Service中的几个重要术语

    WSDL:web service definition language 直译:WebService定义语言 1.对应一种该类型的文件.WSDL 2.定义了Web Service的服务器与客户端应用交 ...

  8. Web Service中的XFire 传输List 自定义对象.

    我把这个创建的步骤和代码的贴出来,. 首先新建一个工程,取名就随便点啦..MyWebService,然后复制jar包到lib目录下, 创建包,建立接口..写一个javaBean的类, 以下是一个简单的 ...

  9. WCF Service 转换为Web Service 中字段属性

    1.新建WCF服务,服务中包含对象 2.部署WCF服务,并将其转换为应用程序 3.通过添加服务引用,使用WCF服务 4.调用对应的对象时需要对应的值设置为True. 参考:https://cloud. ...

随机推荐

  1. [转]How to insert a row between two rows in an existing excel with HSSF (Apache POI)

    本文转自:http://stackoverflow.com/questions/5785724/how-to-insert-a-row-between-two-rows-in-an-existing- ...

  2. HDU 4930 Fighting the Landlords --多Trick,较复杂模拟

    题意:两个人A和B在打牌,只有题目给出的几种牌能出若A第一次出牌B压不住或者A一次就把牌出完了,那么A赢,输出Yes,否则若A牌没出完而且被B压住了,那么A输,输出No. 解法:知道规则,看清题目,搞 ...

  3. UESTC 31 饭卡(Card) --背包问题

    背包问题. 思路:如果m<5,此时也不能消费,所以此时答案为m m>=5: 求出背包容量为m-5,买前n-1样便宜的菜(排个序)的最大价值(即最大消费,即消费完后剩余值最接近5)最后减去最 ...

  4. Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现) .

    今天学习了Spinner组件,使用Spinner相当于从下拉列表中选择项目,下面演示一下Spinner的使用(分别使用ArrayAdapter和自定义Adapter实现) (一):使用ArrayAda ...

  5. 使用Unity开发Android的几种调试方法

    前言 本文举例几种Android 调试的方法(PS:我是通过unity引擎来开发安卓游戏) Eclipse + adt 查看LOG 1.为Eclipse 装上adt 插件 2.打开Eclipse 的L ...

  6. 更改QTP默认测试脚本路径

    QTP的默认测试脚本路径为安装路径下的Tests文件夹下, 如果你安装在D:,那么默认脚本路径为D:\Program Files\HP\QuickTest Professional\Tests 但是因 ...

  7. 使用EasyUI要引入哪些文件

    使用EasyUI,一般需要导入如下文件 <link rel="stylesheet" type="text/css" href="../reso ...

  8. 【C#】【Thread】Monitor和Lock

    所谓锁,就是之锁定的区域只能单个线程进入进行操作,其他线程在锁的外围等待.Monitor锁通过Monitor.Enter(obj)和Monitor.Exit(obj)来锁定和解锁.Lock锁则直接Lo ...

  9. Elasticsearch集群中处理大型日志流的几个常用概念

    之前对于CDN的日志处理模型是从logstash agent==>>redis==>>logstash index==>>elasticsearch==>&g ...

  10. java中从1970-1-1到当前时间之间的毫秒数转换为oracle date

    java中System.currentTimeMillis()取到的是从1970-01-01 00:00:00.000到当前时间的毫秒数,一个long类型的值. 现在oracle数据库中某表中存取的是 ...