asp.net Web服务,生成XML文档时出错

时间:2012-01-12 15:01:02

标签: asp.net web-services xml-serialization asmx

我已经创建了一个asp.net Web服务,并尝试使用我的客户端进行测试。我正在测试的Web方法返回一个名为GetAllTicketsSinceLastPullDateResult的自定义对象,它包含一个自定义clsTrip对象的ArrayList和一个名为FaultResponse的自定义对象。这就是我使用我的客户端的方式:

ServiceReference1.ServiceSoapClient myClient = new ServiceReference1.ServiceSoapClient();
        ServiceReference1.GetAllTicketsSinceLastPullDateResult result = new ServiceReference1.GetAllTicketsSinceLastPullDateResult();

        result = myClient.getAllTicketsSinceLastPullDate(myUser);

但是我收到以下错误:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type clsTicket was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

我谷歌错误和我发现的大多数答案与序列化派生类有关。但我的clsTicket类不是派生类。可能导致此错误的原因是什么?我如何使用XmlInclude或SoapInclude?

提前致谢!

3 个答案:

答案 0 :(得分:3)

好的,我终于得到了它的工作,我最终得到了以下一行:

[XmlInclude(typeof(clsTicket))]

[WebMethod]和我方法的定义之间。到目前为止一切都很好。

答案 1 :(得分:2)

我认为你不需要'新'结果:

ServiceReference1.GetAllTicketsSinceLastPullDateResult result;
result = myClient.getAllTicketsSinceLastPullDate(myUser);

另外,您如何知道服务器上没有错误? 你能用网络浏览器调用这个功能吗?

答案 2 :(得分:1)

您是否在最初添加对项目的引用后更新了服务?如果您正在使用Visual Studio的“添加Web引用”功能并更新服务,则通常会出现配置或参数机会,这可能会生成SOAP错误。

尝试通过visual studio右键单击相关的Web服务,然后选择“更新”选项。重新编译应用程序,看看是否能解决您的问题。