解决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时,默认情 ...
随机推荐
- Android下监听Home键
网上看到demo,亲测了以下机器和设备,均测试通过: 测试通过的手机: 1.华为荣耀3(Android 4.2.2) 2.小米2s(Android 4.1.1) 3.联想的手机 (Android2.3 ...
- Android EventBus源码解析
项目地址 :https://github.com/greenrobot/EventBus 这个项目个人感觉就是为了解决回调事件过多的,比方说A函数在做完以后 要调用b类的c函数,那我们通常的做法就是 ...
- 【转】Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
iOS模拟器简介 iOS功能简介 iOS模拟器,是在Mac下面开发程序时,开发iOS平台的程序时候,可以使用的辅助工具. 其功能是,帮你模拟iOS平台设备,在模拟器上运行对应的程序,以方便你没有实体设 ...
- 对于REST中无状态(stateless)的一点认识
今天早上在Yahoo的邮件列表里看到一篇颇有意思的讨论,标题为RESTful vs. unRESTful: Session IDs and Authentication(51CTO编者注:意为REST ...
- html asp php java 清除缓存
HTML页面 <META HTTP-EQUIV="pragma" CONTENT="no-cache"><META HTTP-EQUIV=&q ...
- 利用 Ant 和 Eclipse 有效地提高部署工作效率
读者定位为具有 Java 和 Ant 使用经验的开发人员. 读者可以学习到如何使用 Ant 解决一些多用户开发环境中,根据不同的目标环境编译成不同部署包的问题. 工作场景 现在有一个 web 项目,是 ...
- nginx upstream的几种配备方式
nginx upstream的几种配置方式 nginx 的upstream目前支持4种方式的分配 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器 ,如果后端服务器down掉,能自动剔 ...
- HDU5807 Keep In Touch (BestCoder Round #86 D ) 分布式dp
#include <cstdio> #include <cstring> #include <cmath> #include <vector> #inc ...
- Php 笔记1-----request和 response
不能大于2KB 第一次学习 php, 因为以前习惯了 .net, 所以 刚开始总是按照.net的 思路去思考, 怎么获取 客户端发过来的 request对象啊, 怎么设置response啊.. ...
- Appium过程中用到的adb点滴知识库
一.认识abd adb是什么? adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是d ...