解决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时,默认情 ...
随机推荐
- Mysql 不同版本 说明
Mysql 的官网下载地址: http://dev.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,免费,但 ...
- [Android] 关于系统工具栏和全屏沉浸模式
随着应用程序的一些深入设计,大家总想要更好的界面和体验,所以有些东西并不能只是知道方法就结束了,是得要去深入研究研究的.通过这个过程我觉得,从应用层面来讲,想实现一个功能很简单,但若想实现的好,就要去 ...
- Python time mktime()方法
描述 Python time mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数. 如果输入的值不 ...
- Eclipse将android项目打包jar文件
Eclipse+android打包jar文件 蔡建良 2016-3-12 以Android-SlideExpandableListView开源框架为例,将源码Library打包成jar文件并包含R.c ...
- Symfony2 学习笔记之内部构件
Symfony2内部是怎样工作的以及我们如何来扩展它呢?从外部整体上看,symfony2代码是由许多独立的层构成,每一层都是建立在前一层基础之上.其中,自动加载时不受框架直接管理的,它完全是在Univ ...
- linux apache 配置fastcgi
Redhat 上 FastCGI 安装与配置 软件包 相关软件包: httpd 2.2.14 //注意版本 这个版本不会出问题 注:apache httpd安装 fcgi-2.4.0.t ...
- Javascript中的Keycode值列表
关于如何得到一个键在Javascript中的Keycode值,可以参考: <body onkeypress=alert(event.keyCode)>请按任意键,你将得到该键的键值! ke ...
- Memcached 内存级缓存
Memcached在大型网站中应用 memcached是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视 频.文件以及 ...
- mysql 插入汉字出现问号 解决方法
mysql中文显示乱码或者问号是因为选用的编码不对或者编码不一致造成的,最简单的方法就是修改mysql的配置文件my.cnf.在[mydqld]和[client]段加入 default-charact ...
- robotframework学习
下载地址: https://pypi.python.org/pypi/robotframework Installation If you already have Python with pip i ...