Web服务序列化复杂对象

时间:2012-04-25 18:19:12

标签: c# web-services serialization web known-types

我有一个Class1的网络服务。 Class1有一个属性的图像列表

[Serializable]
[SoapInclude(typeof(Bitmap))]

public class Class1
{
    private static List<Image> _myList = new List<Image>();

    public List<Image> MyList
    {
        get { return _myList; }

        set
        {
            _myList = value;
        }
    }
}

进行MyList时,会发生以下错误。有什么想法吗?

  

System.Web.Services.Protocols.SoapException:服务器无法执行   处理请求。 ---&GT; System.InvalidOperationException:有一个   生成XML文档时出错。 ---&GT;   System.InvalidOperationException:System.Drawing.Bitmap类型   没想到。使用XmlInclude或SoapInclude属性指定   静态不知道的类型。在   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_Image(字符串   n,String ns,Image o,Boolean isNullable,Boolean needType)at   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_Class1(字符串   n,String ns,Class1 o,Boolean isNullable,Boolean needType)at   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_HelloWorldResponse(对象[]   p)at   Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Serialize(对象   objectToSerialize,XmlSerializationWriter writer)at   System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,   Object o,XmlSerializerNamespaces名称空间,String encodingStyle,   String id)---内部异常堆栈跟踪结束--- at   System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,   Object o,XmlSerializerNamespaces名称空间,String encodingStyle,   字符串id)at   System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,   Object o,XmlSerializerNamespaces名称空间,String encodingStyle)
  在   System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(对象[]   returnValues,Stream outputStream)at   System.Web.Services.Protocols.WebServiceHandler.WriteReturns(对象[]   returnValues)at   System.Web.Services.Protocols.WebServiceHandler.Invoke()---结尾   内部异常堆栈跟踪---

2 个答案:

答案 0 :(得分:2)

我在下面的网址找到了答案。我需要[XmlInclude]标签而不是[SoapInclude]标签

Troubleshooting Common Problems with the XmlSerializer

    [Serializable]
    [XmlInclude(typeof(Bitmap))]
    public class Class1
    {

}

答案 1 :(得分:0)

根据documentation中的示例,我认为您必须在实际的SOAP WebService方法上应用[SoapInclude]属性:

public class MyService : WebService {

    [SoapInclude(typeof(Bitmap))]
    public List<Image> GetImageList() {
        // code here
    }

}