解决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

时间: 2024-07-29 02:11:00

解决WebService 中泛型接口不能序列化问题的相关文章

WebService中Hashtable用法

知识转自这里 code slice 01 1 /// <summary> 2 /// 反序列化数据 3 /// </summary> 4 /// <param name="sXml"></param> 5 /// <param name="type"></param> 6 /// <returns></returns> 7 private T DeSerializer&l

在asp.net webservice中如何使用session

原文:在asp.net webservice中如何使用session 原文:刘武|在asp.net webservice中如何使用session 在使用asp.net编写webservice时,默认情况下是不支持session的,但我们可以把WebMethod的EnableSession选项设为true来显式的打开它,请看以下例子: 1 新建网站WebSite 2 新建web服务WebService.asmx,它具有以下两个方法: C#-Code: [WebMethod(EnableSessio

WebService中方法的相关注意事项

2014-11-14 在WebService中定义方法,有一些注意的地方: (1) 方法上面需要增加 [WebMethod] 属性,标志该方法是一个WebService方法: (2)方法的返回值可以为void.string.int.bool.DataSet等类型,不能为Dictionary<object,object>等特殊类型. 如果真的想使用Dictionary<object,object>字典类型,需要实现相关接口. (3)方法的参数类型必须是可序列化的类型,比如string

解决myeclipse中struts2 bug问题包的替换问题

因为struts2的bug问题,手工替换还是比较麻烦,但即便是最新的myeclipse2014也没有替换最新的struts2包,研究了一天,终于找到了解决办法.以下就解决方法与大家分享一下. 1.在perferences中找到 Myeclipse->Project Libraries,右边找到 struts2.1 Libraries,点击 Enable advanced configiguration,去掉以下文件前面的对勾,然后点击 Add custom Jars 2.在弹出的对话框中选择 A

解决VNC中tab键无效的方法

http://ubuntuforums.org/archive/index.php/t-1771058.html I accidentally discovered a fix for this while trying to solve a different problem.edit~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xmlfind the line <property name="&l

解决Eclipse中C++代码显示Symbol &amp;#39;std&amp;#39; could not be resolved的问题

第一次在Eclipse中写C++代码,写了一个简单的hello world程序,还没有等我编译.就报出了各种错误,但是这么简单的代码.怎么可能这么多错误.于是没有理会.编译执行后,能够正常输出!!!Hello World!!!,但是我的代码中还是有非常多红叉,把鼠标放在上面显示:Symbol 'std' could not be resolved 这种信息. 于是问题来了.怎样解决? 方法:写完库函数以后立刻保存.这样写之后的代码就不会报错了 比如:-->首先写#include <iostre

解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题

解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题http://blog.csdn.net/u012336923/article/details/48289485 /路径/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.1/res/values-v23/v

在Spark中使用Kryo序列化

spark序列化 对于优化<网络性能>极为重要,将RDD以序列化格式来保存减少内存占用. spark.serializer=org.apache.spark.serializer.JavaSerialization Spark默认 使用Java自带的ObjectOutputStream 框架来序列化对象,这样任何实现了 java.io.Serializable 接口的对象,都能被序列化.同时,还可以通过扩展 java.io.Externalizable 来控制序列化性能.Java序列化很灵活但

解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题

解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题 iOS开发 · 2015-01-22 11:31 MWPhotoBrowser是一个非常不错的照片浏览器,在github的star接近3000个,地址:https://github.com/mwaterfall/MWPhotoBrowser.git MWPhotoBrowser来加载小图1M以下的都应该不会有内存警告的问题.如果遇到大图,3M.4M.5M的大图,很有可能导致内存警告.最近我就遇到这个问题,很是头疼