问题:不支持Dictionary;结果:在Web Service中傳送Dictionary
在Web Service中傳送Dictionary
有個需求,想在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:
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法雖有其他缺點,但內建支援的特點,在力求簡單的場合裡,倒也值得納入考量吧!
问题:不支持Dictionary;结果:在Web Service中傳送Dictionary的更多相关文章
- 在Web Service中傳送Dictionary
有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMethod] public Dictionary< ...
- Web Service 中返回DataSet结果大小改进
http://www.cnblogs.com/scottckt/archive/2012/11/10/2764496.html Web Service 中返回DataSet结果方法: 1)直接返回Da ...
- Web Service 中返回DataSet结果的几种方法
Web Service 中返回DataSet结果的几种方法: 1)直接返回DataSet对象 特点:通常组件化的处理机制,不加任何修饰及处理: 优点:代码精减.易于处理,小数据量处理较快: ...
- ASP.NET Web Service中使用Session 及 Session丢失解决方法 续
原文:ASP.NET Web Service中使用Session 及 Session丢失解决方法 续 1.关于Session丢失问题的说明汇总,参考这里 2.在Web Servcie中使用Sessio ...
- 转-Web Service中三种发送接受协议SOAP、http get、http post
原文链接:web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 一.web服务中三种发送接受协议SOAP/HTTP GET/HTTP POST 在web服务中,有三种可供选择的发 ...
- 企业级SOA之路——在Web Service中使用HTTP和JMS
原文:http://www.tibco.com/resources/solutions/soa/enterprise_class_soa_wp.pdf 概述 IT业界在早期有一种误解,认为 ...
- Web Service中的几个重要术语
WSDL:web service definition language 直译:WebService定义语言 1.对应一种该类型的文件.WSDL 2.定义了Web Service的服务器与客户端应用交 ...
- Web Service中的XFire 传输List 自定义对象.
我把这个创建的步骤和代码的贴出来,. 首先新建一个工程,取名就随便点啦..MyWebService,然后复制jar包到lib目录下, 创建包,建立接口..写一个javaBean的类, 以下是一个简单的 ...
- Dictionary(支持 XML 序列化),注意C#中原生的Dictionary类是无法进行Xml序列化的
/// <summary> /// Dictionary(支持 XML 序列化) /// </summary> /// <typeparam name="TKe ...
随机推荐
- JAVA Properties配置文件的读写
通常我们就会看到一个配置文件,比如:jdbc.properties,它是以“.properties”格式结尾的.在java中,这种文件的内容以键值对<key,value>存储,通常以“=” ...
- Vue v-if条件渲染
1.简单的v-if指令,代码如下 <!DOCTYPE html> <html> <head lang="en"> <meta charse ...
- 使用Navicat进行数据库自动备份
今天经历一次数据库丢库事件,顿时觉得定时备份数据库很重要. 但是每天自己手动备份实在是太麻烦了,于是乎,想到用计划任务进行每天定时自动备份. 发现Navicat自带就有备份 还可以直接计划任务,贼方 ...
- mysql数据库优化课程---3、数据库设计是什么
mysql数据库优化课程---3.数据库设计是什么 一.总结 一句话总结: 就是设计各个字段及各个字段类型 1.char,varchar,text同存'ABC'的存储空间为多少? char(255): ...
- numpy常用函数之randn
numpy中有一些常用的用来产生随机数的函数,randn就是其中一个,randn函数位于numpy.random中,函数原型如下: numpy.random.randn(d0, d1, ..., dn ...
- phalcon: 开启模板缓存和缓存路径
/** * 设置view */ $di->set('view', function () use ($config) { $view = new \Phalcon\Mvc\View(); $vi ...
- 《Advanced Bash-scripting Guide》学习(十八):[[ ]]与[ ]的一些特殊情况
本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 例1: [ -eq ] && [ -n "`echo ...
- Docker 介绍安装
简介: Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行 ...
- hdu 5243 Homework
Homework Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- (转)msys2使用教程
一.安装 官方下载地址 http://www.msys2.org/ 指定好安装路径(一般D根目录即可),一路下一步就好. 二.配置国内镜像.设置窗体修改颜色 使用[清华大学开源软件镜像站]中的地址,修 ...