代码之家  ›  专栏  ›  技术社区  ›  Loftx

使用apachesoap:使用.net映射Web服务中的复杂数据类型

  •  3
  • Loftx  · 技术社区  · 17 年前

    我有一个用coldfusion编程的Web服务,我正试图使用c#net来使用它。

    特定的webservices返回一个coldfusion结构(一个包含键和值的项的集合),该结构由webservices作为apachesoap:Map类型的复杂对象公开

    <wsdl:message name="getDetailResponse">
        <wsdl:part name="getDetailReturn" type="apachesoap:Map"/>
    </wsdl:message>
    

    复杂类型在coldfusion自动生成的WSDL文件中正确声明

    <schema targetNamespace="http://xml.apache.org/xml-soap">
        <import namespace="http://webservice.templates"/>
        <import namespace="http://rpc.xml.coldfusion"/>
        <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
        <complexType name="mapItem">
            <sequence>
                <element name="key" nillable="true" type="xsd:anyType"/>
                <element name="value" nillable="true" type="xsd:anyType"/>
            </sequence>
        </complexType>
    
        <complexType name="Map">
    
            <sequence>
                <element maxOccurs="unbounded" minOccurs="0" name="item" type="apachesoap:mapItem"/>
            </sequence>
        </complexType>
    </schema>
    

    当尝试使用以下c#代码使用此代码时:

    theWebservice.theWebservice myWS = new theWebservice.theWebservice();
    theWebservice.Map myMap = myWS.searchForRecord("some record data");
    
    if (myMap.item == null) {
        Response.Write("myMap.item is null");
    }
    

    调试器的检查显示myMap有两个子项和项字段,它们都是WebService.mapItem[]类型,并且都是null值。

    我见过其他论坛帖子也有类似的问题,但没有回复。有人知道我如何正确使用该服务,而不必更改Web服务以仅使用简单类型吗?

    编辑以提供更多信息

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soapenv:Body>
            <ns1:getDetailResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
                <getDetailReturn xsi:type="ns2:Map" xmlns:ns2="http://xml.apache.org/xml-soap">
                    <item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
                        <key xsi:type="soapenc:string">a</key>
                        <value xsi:type="soapenc:string">1</value>
                    </item>
                    <item>
                        <key xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">b</key>
                        <value xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">2</value>
                    </item>
                </getDetailReturn>
            </ns1:getDetailResponse>
        </soapenv:Body>
    </soapenv:Envelope>
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   ryber    16 年前

    您始终可以使用coldfusion web服务,而无需使用.NET内置服务。这需要您手动解析响应,但是,它是XML,所以没有那么糟糕。

    <cfcomponent>
      <cffunction name="GetStruct" access="remote" returntype="struct" output="no">
            <cfscript>
               var struct = StructNew();
               struct.foo = "bar";
               struct.baz = 2;
               struct.Stooges = StructNew();
               struct.Stooges.Larry = 1;
               struct.Stooges.Moe = "Hi Mom";
               struct.Stooges.Curley = "Not Shemp"; 
            </cfscript>
    
        <cfreturn struct>
      </cffunction>
    </cfcomponent>
    

    在.Net中设置您的请求,如下所示:

    var request = WebRequest.Create("http://localhost/test.cfc?method=GetStruct");
    var response = request.GetResponse();
    String content;
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
      content = reader.ReadToEnd();
    }
    

    您返回的内容将是如下所示的wddx数据包:

    <wddxPacket version="1.0">
        <header /> 
        <data>
            <struct>
                <var name="BAZ">
                    <string>2</string> 
                </var>
                <var name="STOOGES">
                    <struct>
                        <var name="MOE">
                          <string>Hi Mom</string> 
                        </var>
                        <var name="CURLEY">
                          <string>Not Shemp</string> 
                        </var>
                        <var name="LARRY">
                          <string>1</string> 
                        </var>
                    </struct>
              </var>
              <var name="FOO">
                <string>bar</string> 
              </var>
            </struct>
        </data>
    </wddxPacket>
    

    当然,更好的解决方案可能是首先返回XML

    另外,您还可以强制coldfusion在cffunction标记上使用returnformat=“JSON”将结构序列化为JSON。

    推荐文章