【问题标题】:SOAP formatting of a WCF serviceWCF 服务的 SOAP 格式
【发布时间】:2014-01-29 17:22:44
【问题描述】:

我们正在尝试开发一种网络服务,它将取代我们客户使用的现有网络服务。这意味着来自 WCF 中新 Web 服务的 SOAP 响应必须与来自当前 Web 服务的 SOAP 响应完全匹配,后者是用 Java 编写的遗留服务。我们无法让新服务返回与旧服务相同的 SOAP 响应。

最初,我们从 DataContract 作为响应的结构开始,但由于它会生成一些额外的标签,我们切换到 MessageContract。

这里是使用 DataContAract 作为请求和响应属性的示例请求和响应。我们在使用 DataContract 时面临两个问题 –

  1. 响应中有两个额外的标签 GetDataUsingDataContractReponse、GetDataUsingDataContractResult,我们无法以编程方式将其删除。

  2. 我们无法将 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


    【解决方案1】:

    过去,当我们的团队需要完全控制 WCF 服务输出时,我们使用 XMLSerializer 而不是数据和/或消息协定。

    WCF 支持 XmlSerializer 类,这不是 WCF 独有的。 XmlSerializer 类支持比 DataContractSerializer 类更窄的类型集,但允许对生成的 XML 进行更多控制,并支持更多 XML 架构定义语言 (XSD) 标准。它也不需要可序列化类型的任何声明性属性。有关详细信息,请参阅 .NET Framework 文档中的 XML 序列化主题。

    参考:
    http://msdn.microsoft.com/en-us/library/ms733901(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多