在使用C#写Web Service时遇到了个很奇怪的问题。返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解。

后来在同事的帮助下,发现了规律,在返回值是泛型的接口前面,只要有返回值是字符串数组的接口,就会发生错误,如果把返回泛型的接口放到返回字符串数组的接口后面,就没问题了。

结合代码说明一下,代码如下:

        [WebMethod]
public string[] HelloWorld1()
{
string[] str = new string[];
str[] = "Hello ";
str[] = "World!";
return str;
} [WebMethod]
public List<string> HelloWorld()
{
List<string> strList = new List<string>();
strList.Add("Hello ");
strList.Add("World!");
return strList;
}

此时调用HelloWorld就会出现错误,错误如下

System.InvalidOperationException: 生成 XML 文档时出错。 ---> System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”。
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_ArrayOfString(Object o)
在 Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfStringSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
在 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
在 System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
在 System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
在 System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
在 System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
在 System.Web.Services.Protocols.WebServiceHandler.Invoke()

     将HelloWorld和HelloWorld1都改为string数组或List<string>泛型就没有问题了。 本人觉得这个问题很奇怪,可能是VS2008的bug。写出来只是希望大家在使用时能够注意这个情况。

解释如下:

这是不是BUG,因为你是用了泛型,所以在序列化的时候要指定它的类型,加上:
[XmlInclude(typeof(类型))] 就OK了。

感觉不是List不能序列化的问题,而是List在序列化以后,他的本质还是array,因此,如果在一个Webservice里存在两种序列化方式的的方法,就会存在问题。

Web Service在传输对象的时候,是把对象先序列化成为xml或者数据流,string对象是可以序列化,而List不是可序列化对象,所以在System.Xml.Serialization.XmlSerializer.Serialize 序列化的时候出错了

Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)的更多相关文章

  1. 无法将类型为“System.Decimal”的对象强制转换为类型“System.Char[]”。

    在用微软的SSIS操作ORACLE 数据源的时候碰到以下报错信息: [ADO NET Destination [13455]] 错误: 数据插入期间出现异常,从提供程序返回的消息为:无法将类型为&qu ...

  2. 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1

    在WPF中DataGrid 选择事件中获取SelectedItems 报错如下 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转 ...

  3. 【Winform】 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”。

    问题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Window ...

  4. 无法将类型为“System.DBNull”的对象强制转换为类型“System.String”

    在ERP中做业务类单据,有时候会遇到这样的报错. 无法将类型为"System.DBNull"的对象强制转换为类型"System.String"   去数据库中检 ...

  5. WPF System.InvalidCastException: 无法将类型为“System.Windows.Media.Color”的对象强制转换为类型“System.Windows.Media.Brush”。

    场景:添加ComboBox样式,界面卡死,日志异常文件如下: -- ::, | ERROR | System.InvalidCastException: 无法将类型为“System.Windows.M ...

  6. 保存图片控件上的图片到本地 出现错误:无法将类型为“System.Windows.Media.Imaging.BitmapFrameDecode”的对象强制转换为类型“System.Windows.Media.Imaging.BitmapImage”。

    保存图片控件上的图片到本地 出现错误:无法将类型为“System.Windows.Media.Imaging.BitmapFrameDecode”的对象强制转换为类型“System.Windows.M ...

  7. 如何解决”无法将类型为“System.DateTime”的对象强制转换为类型“System.String”。“

    字段Time在数据库中为datetime类型 dr.GetString(3).ToString() dr.GetString(3).ToString() => dr.GetDateTime(3) ...

  8. c#:无法将类型为“System.DBNull”的对象强制转换为类型“System.String”

    解决办法: 使用转换函数即可: Convert.ToString(要转换的值);

  9. 无法将类型为“System.Xml.XmlComment”的对象强制转换为类型“System.Xml.XmlElement”

    今天开发C#项目时,有一个需要读取XML的功能点.编码过程中遇到了如标题所示的异常,如下图所示: 查询官网后得知XmlComment是注释节点的类型,如下图所示: 于是得出结论,使用XmlDocume ...

随机推荐

  1. MongoDB的.Net驱动

    mongo的驱动主要使用了两个,即samus和官方驱动. 个人感觉差别不大,且官方驱动也支持了LinQ.但在使用DBRef的时候,发现samus的驱动似乎不太好用,并没有达到想要的效果,也许是我的使用 ...

  2. 安装使用yii-debug-toolbar,yii框架的调试插件

    1github 下载yii-debug-toolbar 插件 地址:https://github.com/malyshev/yii-debug-toolbar 2. 假设yii的项目创建在了/expo ...

  3. TortoiseSVN使用方法 安装和配置

    TortoiseSVN使用方法   安装和配置 TortoiseSVN的下载地址为 http://tortoisesvn.net/downloads.html 有32位和64位的版本,一定要根据自己的 ...

  4. jQuery Fancybox插件介绍

    下面介绍一款jquery图片播放插件叫Fancybox,项目主页地址:http://fancybox.net/ Fancybox的特点如下: 1.可以支持图片.html文本.flash动画.ifram ...

  5. JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案

    JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案 最近在开发WSS RESTful服务的时候, 碰到了这些个纠 ...

  6. Java笔记:与系统交互、系统相关的类,Object类

    1.程序与用户交互 (1)运行一个Java程序的时候要给它提供一个main方法入口,这边分析一下这个main方法的签名public static void main(String[] args);pu ...

  7. MVC+Front Controller

    MVC+Front Controller 在我前面一篇博文<逃脱Asp.Net MVC框架的枷锁,使用Razor视图引擎>发表之后,很多人关心,脱离了之后怎么办?那么这可以说是它的续篇了. ...

  8. .Net项目框架

    摘要:本文描述了在用VS.NET进行B/S开发时采用的框架结构,一般建立类库项目和Web项目,在Web基本aspx页面类中调用类库中方法,同时在aspx页面类中不需要写任何对数据库操作的SQL代码,便 ...

  9. PHP, Python Nginx works together!

    Nginx is so good at delivering requests to many others. Good! Now let's use the nginx upstream modul ...

  10. MBR,boot loader, partition table, backup, recovery, clean 硬盘引导记录,分区表备份,恢复,清空

    linux/unix系统的分区表结构位于硬盘开始的MBR上.MBR上大小为512bytes. MBR=446B启动信息+64B分区表+2B分隔符=512字节 前面的446bytes作为启动信息,启动信 ...