【问题标题】:iphone app pass object array to .net SOAP webservice as parameter not recognizableiphone应用程序将对象数组传递给.net SOAP webservice作为参数无法识别
【发布时间】: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


    【解决方案1】:

    我真的不知道如何解决您的问题,但以防万一您无法解决问题并且需要解决问题,请查看http://sudzc.com/,它将生成您提出请求所需的所有代码目标 C. 我用过,在发送数组时也遇到了一些问题(使用sudzc自动生成的类),但我解决了this way

    【讨论】:

    • 非常感谢您的回复。我现在正在做的 Web 服务只在我们的本地测试服务器上,所以我需要将它发布到生产环境中。我会试一试。谢谢。
    【解决方案2】:

    这是我的问题的解决方案。即使我不确切知道为什么我的代码不起作用,但我知道当参数传递给 .net 服务器时,它试图猜测我传递的对象是什么。不知何故,即使自定义类已在 Web 方法的开头定义和声明,它也会对自定义对象数组感到困惑。

    为了解决真正的问题,我有几个选择: 将 xml 作为字符串传递并在服务器端手动对其进行反序列化。 将数据集传递给服务器。 将我的类定义为 XMLNode 并将其传递给服务器。

    无论我选择哪种方式,我都必须做一些工作才能正确反序列化xml。

    还有另一种方法。

    因为只有当我对我的 SOAP 消息使用 RPC 格式时服务器才会感到困惑,那么另一种格式呢?我尝试了下面的代码,然后宾果游戏,服务器立即拿起它。

     [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>
    

    在这里我要特别感谢 Peter pi - Microsoft 的 MSFT 帮助我解决了这个问题,这里是 asp.net 上的链接:

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

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

    【讨论】:

    • 如果数组是字符串类型,我该怎么做?像这样? team1team2
    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2012-01-25
    • 2023-03-04
    • 2021-07-29
    • 1970-01-01
    • 2020-02-12
    相关资源
    最近更新 更多