iphone应用程序将对象数组传递给.net SOAP webservice作为参数不可识别

问题描述:

我在写一个ios应用程序,它使用SOAP Web服务与asp.net服务器通信。我的一个Web服务需要使用一组对象。两边的对象定义完全相同。iphone应用程序将对象数组传递给.net SOAP webservice作为参数不可识别

这是我试过的:当我只传递一个对象作为参数时,web服务工作正常。但是,只要我传递了一组对象,我什么也没有返回。我知道Web服务中的代码永远不会被调用,这意味着服务器无法读取参数。有线的事情是,网络服务没有返回任何东西,所以我不知道什么是错的(当我在过去做错了事情时,我曾经从服务器获取错误消息,向我展示堆栈跟踪)。

我对SOAP Web服务并不熟悉,所以即使我在MSDN上花了很多时间,我仍然不明白什么是错的。发布Web服务后,我通过浏览器访问它。我把整个东西都复制到我的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: 

这是我的问题的解决方案。尽管我不知道为什么我的代码不起作用,但我知道的是当参数传递给.net服务器时,它试图猜测我传递的对象是什么。即使自定义类已经在web方法的开始处定义并声明,它也会混淆自定义对象的数组。

为了解决实际问题,我有几个选择: < 1>将xml作为字符串传递并手动将其反序列化到服务器端。 < 2>将数据数据集传递给服务器。 < 3>将我的类定义为XMLNode并将其传递给服务器。

不管我选择哪种方式,我必须做一些工作来正确地反序列化xml。

还有另一种方法可以做到这一点。

由于服务器只有迷茫的时候我用RPC格式为我的SOAP消息,那另一种格式?我尝试了下面的代码,并且宾果,服务器马上拿起它。

[WebMethod] 
public string testTeamWebService(Team[] teams) 
{ 
    // do something here 
} 


// class definition of Team 
[Serializable] 
public class Team 
{ 
    public int TeamID; 
    public string TeamName; 
} 

,然后将XML请求以这种方式格式化:

<?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:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<testTeamWebService xmlns="http://tempuri.org/"> 
    <teams> 
    <Team> 
     <TeamID>int</TeamID> 
     <TeamName>string</TeamName> 
    </Team> 
    <Team> 
     <TeamID>int</TeamID> 
     <TeamName>string</TeamName> 
    </Team> 
    </teams> 
</testTeamWebService> 

在这里,我想特别感谢彼得PI - 从微软(MSFT)的帮助我解决这个问题,这里是asp.net上的链接:

http://forums.asp.net/t/1784291.aspx/1?asp+net+SOAP+RPC+web+method+could+not+read+object+array+parameter

希望我的帖子能帮助一些人谁遇到同样的问题。

+0

如果数组的类型是字符串,我会怎​​么做?喜欢这个? TEAM1 TEAM2 Skychan 2017-10-13 16:03:53

+0

更新:以上完全为我工作 – Skychan 2017-10-13 16:47:59

我真的不知道如何解决你的问题,但为了以防万一,你不能和需要得到它,取看看http://sudzc.com/这将产生你需要在目标c中提出请求所需的所有代码。 我用它,我发送阵列也有一些问题(使用sudzc自动生成的类),但我解决了它们this way

+0

非常感谢您的回复。我现在正在执行的Web服务仅在我们的本地测试服务器上,因此我需要将其发布到生产环境中。我会试一试。谢谢。 – 2012-03-23 14:20:22