【发布时间】:2012-03-22 21:35:31
【问题描述】:
我正在编写一个使用 SOAP Web 服务与 asp.net 服务器通信的 ios 应用程序。我的一项网络服务需要获取对象数组。双方的对象定义完全相同。
这是我尝试过的:当我只传递一个对象作为参数时,Web 服务运行良好。但是,一旦我传递了一个对象数组,我什么也没有返回。我知道 Web 服务中的代码永远不会被调用,这意味着服务器无法读取参数。连线的事情是 Web 服务没有返回任何内容,所以我无法判断出了什么问题(过去,当我做错事时,我曾经从服务器收到错误消息,显示堆栈跟踪)。
我对 SOAP Web 服务不是很熟悉,所以即使我在 MSDN 上花了很多时间,我仍然不明白哪里出了问题。发布网络服务后,我通过浏览器访问它。我将整个内容复制到了我的 iOS 应用程序中,所以理论上它应该可以工作,但它从来没有。
不管怎样,这是服务器端代码:
[System.Web.Services.Protocols.SoapRPCMethod]
[WebMethod(EnableSession = true)]
[XMLInclude(typeof(Team))]
[XMLInclude(typeof(Team[]))]
[SoapInclude(typeof(Team))]
[SoapInclude(typeof(Team[]))]
public string testTeamWebService(Team[] teams)
{
// do something here
}
// class definition of Team
[Serializable]
public class Team
{
public int TeamID;
public string TeamName;
}
根据 web 服务页面(.asmx 文件),我应该这样做:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="https://abc.com/" xmlns:types="https://abc.com/encodedTypes" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:testTeamWebService>
<teams href="#id1" />
</tns:testTeamWebService>
<soapenc:Array id="id1" soapenc:arrayType="types:Team[2]">
<Item href="#id2" />
<Item href="#id3" />
</soapenc:Array>
<types:Team id="id2" xsi:type="types:Team">
<TeamID xsi:type="xsd:int">int</TeamID>
<TeamName xsi:type="xsd:string">string</TeamName>
</types:Team>
<types:Team id="id3" xsi:type="types:Team">
<TeamID xsi:type="xsd:int">int</TeamID>
<TeamName xsi:type="xsd:string">string</TeamName>
</types:Team>
</soap:Body>
</soap:Envelope>
正如我所说,我构建了 xml 并将其发送到 iOS。我使用了 NSMutableURLRequest 对象,构造的 xml 正是我上面提到的方式。
在我的其他 Web 服务中,我从服务器获取对象数组,因此我知道 .net 可以序列化数组。这是我的服务第一次需要将数组作为参数,所以我认为一定有办法做到这一点。
感谢您的阅读,如果可以的话,请给我一些建议。
我终于收到了来自服务器的错误信息:
faultcode: soap:Server
faultstring: System.Web.Services.Protocols.SoapException: Server was unable to process request.---> System.ArgumentException: Object of type 'System.Xml.XmlNode[]' cannot be converted to type 'abc.WebServices.Team[]'.
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Web.Services.Protocols.LogicalMethodInfo.Invoke(Object target, Object[] values)
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
--- End of inner exception stack trace ---
detail:
【问题讨论】:
标签: iphone .net ios web-services soap