解决WebService 中泛型接口不能序列化问题
本来要定义WebServices 方法返回一泛型接口集合IList,系统提示不能序列化泛型接口集合
1 [WebMethod]
2 public IList<Employee> GetEmployeeList()
3 {
4 IFormatter formatter = new SoapFormatter();
5 MemoryStream mStream = new MemoryStream();
6
7 Employee em1 = new Employee();
8 em1.EmployeeID = 1;
9 em1.FirstName = "jack";
10 em1.LastName = "josn";
11 IList<Employee> list = new List<Employee>();
12 list.Add(em1);
13 list.Add(em2);
14 return list;
15
参考了相关的资料,可以有两种解决办法,一:用List<>泛型集合替代IList<>泛型接口集合。
二.将List<>泛型集合序列化为二进制形式,进行传递。
1 /// <summary>
2 /// List泛型集合替代IList
3 /// </summary>
4 /// <returns></returns>
5 [WebMethod]
6 public List<Employee> GetEmployeeList()
7 {
8 IFormatter formatter = new SoapFormatter();
9 MemoryStream mStream = new MemoryStream();
10
11 Employee em1 = new Employee();
12 em1.EmployeeID = 1;
13 em1.FirstName = "jack";
14 em1.LastName = "josn";
15 List<Employee> list = new List<Employee>();
16 list.Add(em1);
17 return list;
18 }
19
20 /// <summary>
21 /// 以二进制形式进行传递,客户端需进行返序列化
22 /// </summary>
23 /// <returns></returns>
24 [WebMethod]
25 public byte[] GetEmployeeListByteArray()
26 {
27 Employee em1 = new Employee();
28 em1.EmployeeID = 1;
29 em1.FirstName = "jack";
30 em1.LastName = "josn";
31 IList<Employee> list = new List<Employee>();
32 list.Add(em1);
33 IFormatter formatter = new BinaryFormatter();
34 MemoryStream mStream = new MemoryStream();
35 byte[] bs;
36 if (list != null)
37 {
38 formatter.Serialize(mStream,list);
39 bs = mStream.ToArray();
40 }
41 else
42 {
43 bs = new byte[0];
44 }
45 return bs;
46
客户端反序列化代码
1 protected void CallService()
2 {
3 WebService ws = new WebService();
4 byte[] bs = ws.GetEmployeeListByteArray();
5 IList<Employee> list = null;
6 try
7 {
8 MemoryStream ms = new MemoryStream(bs); //创建Memory流对象
9 BinaryFormatter formatter = new BinaryFormatter();
10 list = (List<Employee>)formatter.Deserialize(ms); //反序列化
11 }
12 catch (Exception ex)
13 {
14 Response.Write("<script language='javaScript'>alert('"+ex.Message+"');</script>");
15 }
16
非泛型集合的IList接口进行传递时,只需在方法前标识[XmlInclude(typeof(类型)]即可。
1 [WebMethod]
2 [XmlInclude(typeof(Employee))]
3 public IList GetArticleList()
4 {
5 IList result = new ArrayList();
6 for (int i = 0; i < 20; i++)
7 {
8 DateTime time = DateTime.Now.AddDays(i);
9 Employee em = new Employee();
10 em.LastName = "jack";
11 em.EmployeeID = 11;
12 result.Add(em);
13 }
14 return result;
15 }
16
解决WebService 中泛型接口不能序列化问题的更多相关文章
- 解决WebService中System.InvalidOperationException:缺少参数的问题
此问题在.Net 4.0 IIS7 Windows Server 2008下可能会出现. 现象是第一次正常调用,第二次接口报错. 删除CacheDuration即可.
- WebService中方法的重载
阅读目录 一:WebService中的方法是否允许重载? 二:为什么WebService中不支持方法的重载? 三:如何解决WebService中方法的重载? 一:WebService中的方法是否允许重 ...
- WebService中使用自定义类的解决方法(5种)
转自:http://www.cnblogs.com/lxinxuan/archive/2007/05/24/758317.html Demo下载:http://files.cnblogs.com/lx ...
- 解决nodejs中json序列化时Date类型默认为UTC格式
在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 上面只是一个例子,下面我用一个更具体化的例子来展示一个这个情况,我们在开发WEB项目中,经常用到Express组件, 我 ...
- 解决nodejs中json序列化时Date类型为UTC格式
在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 zhupengfei@DESKTOP-HJASOE3 MINGW64 /d/MyProject/exp2 $ node ...
- 处理 WebService 中的 Map 对象
最近,我们讨论了关于 WebService 的相关问题.目前在 Smart 中,可发布两种类型的 WebService,它们是:SOAP 服务 与 REST 服务,您可以根据需要自由选择. 今天,我要 ...
- C# Webservice中如何实现方法重载--(方法名同名时出现的问题)
本文摘抄自:http://blog.sina.com.cn/s/blog_53b720bb0100voh3.html 1.Webservice中的方法重载问题(1)在要重载的WebMethod上打个M ...
- WebService中方法的相关注意事项
2014-11-14 在WebService中定义方法,有一些注意的地方: (1) 方法上面需要增加 [WebMethod] 属性,标志该方法是一个WebService方法: (2)方法的返回值可以为 ...
- 在asp.net webservice中如何使用session
原文:在asp.net webservice中如何使用session 原文:刘武|在asp.net webservice中如何使用session 在使用asp.net编写webservice时,默认情 ...
随机推荐
- 【英语】Bingo口语笔记(34) - Hit系列
hit it off 合得来 hit the bottle 喝醉酒 hit the spot 正合要求,恰到好处
- 使用sqlldr将文件中的数据导入到数据库
1.创建数据文件: ?如,在D:\创建 zhaozhenlong.txt 文件,文件内容为: 11,12,1321,22,2331,32,33 2.创建控制文件: 如,在D:\创建 zhaozhenl ...
- 一种Javascript解释ajax返回的json的好方法
通常ajax请求返回的格式为json或者xml,如果返回的是json,则可以通过转换成javascript对象进行操作,如下: 1.ajax请求的controller实现 @RequestMappin ...
- zabbix (2.0.6) 历史记录处乱码
1.首先备份数据库 mysqldump -uroot -p123456 zabbix > zabbix.sql 2.设置字符 sed -i 's/latin1/utf8/g' zabbix.sq ...
- 【转】C# 委托的介绍(delegate、Action、Func、predicate)
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Delegat ...
- vector容器总结.xml
1 清空所有元素 m_itemVector.clear(); 2 遍历 vector<ITEM_CHECK>::iterator iter=m_itemVector.b ...
- Autodesk Stingray 游戏引擎
Autodesk的游戏引擎质量够高的. http://v.youku.com/v_show/id_XMTMwMjc0MDIwMA==.html?qq-pf-to=pcqq.group http://v ...
- 关于python中的unicode字符串的使用
基于python2.7中的字符串: unicode-->编码encode('utf-8')-->写入文件 读出文件-->解码decode('utf-8')-->unicode ...
- while (cin>>str)退出死循环
今天在练习的时候突然发现了这个问题,百度之感觉还挺常见的,故记之! //题目描述 // //写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串. // //输入描述 : //输入一个 ...
- WCF基礎
參考:http://www.cnblogs.com/MeteorSeed/archive/2012/04/24/2399455.html http://www.cnblogs.com/scy25114 ...