【发布时间】:2014-01-29 17:22:44
【问题描述】:
我们正在尝试开发一种网络服务,它将取代我们客户使用的现有网络服务。这意味着来自 WCF 中新 Web 服务的 SOAP 响应必须与来自当前 Web 服务的 SOAP 响应完全匹配,后者是用 Java 编写的遗留服务。我们无法让新服务返回与旧服务相同的 SOAP 响应。
最初,我们从 DataContract 作为响应的结构开始,但由于它会生成一些额外的标签,我们切换到 MessageContract。
这里是使用 DataContAract 作为请求和响应属性的示例请求和响应。我们在使用 DataContract 时面临两个问题 –
响应中有两个额外的标签
GetDataUsingDataContractReponse、GetDataUsingDataContractResult,我们无法以编程方式将其删除。我们无法将
BoolValue属性作为 XML 属性,而是显示为 XML 元素。
如果您能分享解决这两个问题的任何想法,我们将不胜感激。以下是使用DataContract 的示例请求和响应。
使用 DataContract 请求-
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:mem="http://schemas.datacontract.org/2004/07/MemoPayment">
<soapenv:Header/>
<soapenv:Body>
<tem:GetDataUsingDataContract>
<!--Optional:-->
<tem:composite>
<mem:BoolValue>true</mem:BoolValue> <!--Not able to make this value as XML Attribute. Instead, always shows up as XML Element -->
<mem:StringValue>Sam</mem:StringValue>
</tem:composite>
</tem:GetDataUsingDataContract>
</soapenv:Body>
</soapenv:Envelope>
使用 DataContract 响应:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetDataUsingDataContractResponse xmlns="http://tempuri.org/">
<GetDataUsingDataContractResult xmlns:a="http://schemas.datacontract.org/2004/07/MemoPayment" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> à Extra tags showing up. Any way to remove them?
<a:BoolValue>true</a:BoolValue>
<a:StringValue>SamSuffix</a:StringValue>
</GetDataUsingDataContractResult>
</GetDataUsingDataContractResponse>
</s:Body>
</s:Envelope>
为了更好地控制 SOAP 消息,我们已从 DataContract 切换到 MessageContract,现在响应正在按预期呈现 XML 属性和 XML 元素。
但是,使用MessageContract 的问题是所有XML 元素都显示为可选。示例 – 以下请求中的 StringValue。同样,如果您能根据您的经验提出任何解决方法,我们将不胜感激。
使用 MessageContract 请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:MsgRequest boolVal="true">
<!--Optional:-->
<tem:StringValue>Sam</tem:StringValue> à Always showing up as Optional. Not able to control from the code. Any way to remove the Optional attribute?
</tem:MsgRequest>
</soapenv:Body>
</soapenv:Envelope>
使用 MessageContract 响应:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MsgResponse boolVal="true" xmlns="http://tempuri.org/">
<StringValue>SamSuffix</StringValue>
</MsgResponse>
</s:Body>
</s:Envelope>
【问题讨论】:
标签: .net wcf web-services serialization soap