gpt4 book ai didi

.net - 使用 apachesoap :Map complex datatype in webservice using .net

转载 作者:行者123 更新时间:2023-12-02 02:36:53 27 4
gpt4 key购买 nike

我有一个用 coldfusion 编程的网络服务,我正试图使用​​ c#.net 使用它。

特定的 web 服务返回一个 coldfusion 结构(具有键和值的项的集合),该结构由 web 服务公开为 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.item is null”而不是具有键值对的对象。

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

我看过其他有类似问题但没有回复的论坛帖子,有谁知道我如何才能正确使用该服务而不必更改 web 服务以仅使用简单类型?

编辑以提供更多信息

根据 John Saunders 的问题,我在 Visual Web Developer 2008 中使用 .NET Framework 3.5。web 服务作为 Web 引用包含在内,下面提供了响应 SOAP 代码(来自 soapUI):

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

最佳答案

您始终可以使用 coldfusion 网络服务,而无需使用 .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 开始

附言您还可以使用 cffunction 标记上的 returnformat="json"强制 coldfusion 将结构序列化为 JSON。

关于.net - 使用 apachesoap :Map complex datatype in webservice using .net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1132536/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com