在使用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. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  2. 浅谈一下SSI+Oracle框架的整合搭建

    浅谈一下SSI+Oracle框架的整合搭建 最近换了一家公司,公司几乎所有的项目都采用的是Struts2+Spring+Ibatis+Oracle的架构,上一个东家一般用的就是JSF+Spring,所 ...

  3. zookeeper使用跟原理

    zookeeper使用和原理 zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<The Chu ...

  4. linux下安装PHP5.5

    下载安装包后,在安装php之前必须先安装libxml2,因此可以通过下载libxml2安装包,编译安装 我通过yum -y install libxml2 libxml2-devel(不安装这个的话, ...

  5. 语音语音合成科大讯飞和Tizen-TTS语音合成引擎

    废话就不多说了,开始...      最近在做一个文本转语音TTS(Text to Speech)的第三方软件封装,应用的是海内语音技术龙头安徽科大讯飞公司提供的离线引擎AiSound5.0,重要用于 ...

  6. 关于EL表达式的生效时间(猜想)

    通过ajax与服务端异步交互的时候,在服务端将某些变量或对象设置到request等域里,此时页面上的EL表达式是获取不到ajax异步交互时设置在request等域里的变量或对像的. 我猜测可能EL表达 ...

  7. Mvc分页组件MvcSimplePager代码重构

    1 Mvc分页组件MvcSimplePager代码重构 1.1 Intro 1.2 MvcSimplePager 代码优化 1.3 MvcSimplePager 使用 1.4 End Mvc分页组件M ...

  8. Testlink1.9.14介绍及使用

    Testlink简介  今天在为部门培训testlink的使用,顺便把相关片段记录下来. TestLink是一款开源的测试管理工具,主要用于进行测试过程的管理,通过使用TestLink提供的功能,可以 ...

  9. [CSS] 子元素垂直居中的两种方式

    1. 多个子元素水平并排,IE10以下失效 display: flex; align-items: center; justify-content: center; 2.多个子元素竖直排列,这种方式会 ...

  10. java的return区别

    return ;和return null的区别在于:前者当方法返回值为void时候,return ; 跳出方法. 后者当方法的返回值为object对象时,return null,跳出方法,返回值为空值 ...