本来要定义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. MVC-READ5(asp.net web from PK asp.net MVC)

    webform: ViewState问题 页面生命周期 不能很好的分解关注点 对HTML操控受限 抽象不完全 可测试性弱

  2. DataTable反向模糊匹配查找语法

    正向写法: string filter = "code like '%"+sheetname+"%'"; filter值为: code like '%表F.3_ ...

  3. 构建通过 Database.com 提供技术支持的 PhoneGap 应用程序

    要求 其他必要产品 Database.com account 用户级别 全部 必需产品 PhoneGap Build 范例文件 Database.Com-PhoneGap-Sample 在这篇文章中, ...

  4. base64 encoding

    //https://en.wikipedia.org/wiki/Base64 std::string base64Encode(const std::vector<char>& b ...

  5. DBus学习笔记

    摘要:DBus作为一个轻量级的IPC被越来越多的平台接受,在MeeGo中DBus也是主要的进程间通信方式,这个笔记将从基本概念开始记录笔者学习DBus的过程 [1] DBus学习笔记一:DBus学习的 ...

  6. C#读写文件总结

    1.使用FileStream读写文件   文件头:   using System; using System.Collections.Generic; using System.Text; using ...

  7. LR之脚本调试

    1.概述 2.Animated run和Non-animated run 3.调试小技巧 4.日志设置

  8. python 数字和字符串转换问题

    一.python中字符串转换成数字 (1)import string tt='555' ts=string.atoi(tt) ts即为tt转换成的数字 转换为浮点数 string.atof(tt) ( ...

  9. iOS优秀博客收录(持续更新)

    唐巧 王巍 破船之家 NSHipster Limboy 无网不剩 念茜的博客 Xcode Dev Ted’s Homepage txx’s blog KEVIN BLOG 阿毛的蛋疼地 亚庆的 Blo ...

  10. Fedora20 和ubuntu 14.04 chrome标签中文乱码

    作为两个流行的桌面发行版本,Fedora和ubuntu最新版本都存在chrome标签中文乱码问题. 下面是解决办法,都来自百度贴吧. 1.ubuntu 系列: 解决办法就是: 编辑/etc/fonts ...