本来要定义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 中泛型接口不能序列化问题的更多相关文章

  1. 解决WebService中System.InvalidOperationException:缺少参数的问题

    此问题在.Net 4.0 IIS7 Windows Server 2008下可能会出现. 现象是第一次正常调用,第二次接口报错. 删除CacheDuration即可.

  2. WebService中方法的重载

    阅读目录 一:WebService中的方法是否允许重载? 二:为什么WebService中不支持方法的重载? 三:如何解决WebService中方法的重载? 一:WebService中的方法是否允许重 ...

  3. WebService中使用自定义类的解决方法(5种)

    转自:http://www.cnblogs.com/lxinxuan/archive/2007/05/24/758317.html Demo下载:http://files.cnblogs.com/lx ...

  4. 解决nodejs中json序列化时Date类型默认为UTC格式

    在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 上面只是一个例子,下面我用一个更具体化的例子来展示一个这个情况,我们在开发WEB项目中,经常用到Express组件, 我 ...

  5. 解决nodejs中json序列化时Date类型为UTC格式

    在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 zhupengfei@DESKTOP-HJASOE3 MINGW64 /d/MyProject/exp2 $ node ...

  6. 处理 WebService 中的 Map 对象

    最近,我们讨论了关于 WebService 的相关问题.目前在 Smart 中,可发布两种类型的 WebService,它们是:SOAP 服务 与 REST 服务,您可以根据需要自由选择. 今天,我要 ...

  7. C# Webservice中如何实现方法重载--(方法名同名时出现的问题)

    本文摘抄自:http://blog.sina.com.cn/s/blog_53b720bb0100voh3.html 1.Webservice中的方法重载问题(1)在要重载的WebMethod上打个M ...

  8. WebService中方法的相关注意事项

    2014-11-14 在WebService中定义方法,有一些注意的地方: (1) 方法上面需要增加 [WebMethod] 属性,标志该方法是一个WebService方法: (2)方法的返回值可以为 ...

  9. 在asp.net webservice中如何使用session

    原文:在asp.net webservice中如何使用session 原文:刘武|在asp.net webservice中如何使用session 在使用asp.net编写webservice时,默认情 ...

随机推荐

  1. 【解题报告】[动态规划] RQNOJ - PID15 / 采药

    原题地址:http://www.rqnoj.cn/problem/15 好久以前做的题了,是个背包问题,就不解释了. #include<stdio.h> #define MAX 100 i ...

  2. 一:ZooKeeper简介

    一:背景                --->随着互联网技术的高速发展,企业对计算机系统的计算,存储能力要求越来越高,最简单的明证就是出现一些诸如:高并发,海量存储这样的词汇.在这样的背景下, ...

  3. Java调优之jvm和线程的内存分析

    本文来源于铁木箱子的博客http://www.mzone.cc 这几天因为自己开发的一个网站在768M内存的机器上撑不起100多个用户的运行,因为每个用户启用功能后,系统将为每个用户分配8个左右的独立 ...

  4. Java循环语句之 for

    Java 的循环结构中除了 while 和 do...while 外,还有 for 循环,三种循环可以相互替换. 语法: 执行过程: <1>. 执行循环变量初始化部分,设置循环的初始状态, ...

  5. 通过文件流stream下载文件

    public ActionResult ShowLocalizedXML(int id) { string orderName = ""; string xmlString = G ...

  6. Bootstrap-select:美化原生select

    官网:http://silviomoreto.github.io/bootstrap-select/ 1.下载zip 2.html代码 <select class="selectpic ...

  7. JSTL笔记(胖先生版)

    今天系统的学习了一下jstl,来记录一下. 在学习jstl以前,先要引两个jar包,然后再加入标签: <%@ taglib prefix="c" uri="http ...

  8. web医疗影像浏览demo及地址

    各种web影像浏览demo及地址1.WPACS http://demo.dayisheng.com 帐号密码 cc dd(http://demo.dayisheng.com/wpacs33.aspx? ...

  9. yii中设置提示成功信息,错误提示信息,警告信息

    方法一: <?phpYii::app()->user->setFlash(‘success’,”Data saved!”); 设置键值名为success的临时信息.在getFlash ...

  10. 自定义控件如何给特殊类型的属性添加默认值 z

    定义控件如何给特殊类型的属性添加默认值了,附自定义GroupBox一枚 标题有点那啥,但确实能表达我掌握此法后的心情. 写自定义控件时往往会有一个需求,就是给属性指定一个默认值(就是可以在VS中右键该 ...