WCF肥皂反应 - 删除"结果"节点(格式WCF响应)

时间:2015-03-26 10:16:36

标签: c# .net wcf soap idispatchmessageinspector

我正在将旧版Web服务转换为WCF服务。此服务目前正由前端应用程序使用。但截至目前,前端有意使用新的WCF服务,因此我们计划将请求从IIS重新路由到我们的新服务。这里的问题是新的WCF服务应该能够处理旧的输入请求,并且应该能够以完全相同的格式发回响应。

我在处理输入请求时没有遇到任何问题,但我遇到了尝试以旧格式返回消息的问题。我当前的服务以下面的格式返回SoapResponse(我没有使用任何消息契约/ DataContracts,因为我使用旧遗留代码中使用的相同返回类型和输入参数):

<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">
  <UserCheckResponse xmlns="Abc.SomeNamespace">
     <UserCheckResult a:Direction="Response" a:Purpose="UserCheck" xmlns:a="Abc.SomeNamespace">
        <a:KV a:Key="TraceID" a:Value="546546565" />
        <a:KV a:Key="Response" a:Value="78954" />
        <a:KV a:Key="UserVerified" a:Value="N" />
        <a:KV a:Key="TryAgain" a:Value="Y" />
        <a:KV a:Key="DataSource" a:Value="NA" />
     </UserCheckResult>
  </UserCheckResponse>

 

我希望它看起来如下(我不想在响应中使用UserCheckResult节点,而是直接在UserCheckResponse节点下想要键值对):

<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">
  <UserCheckResponse a:Direction="Response" a:Purpose="UserCheck" xmlns:a="Abc.SomeNamespace">
        <a:KV a:Key="TraceID" a:Value="546546565" />
        <a:KV a:Key="Response" a:Value="78954" />
        <a:KV a:Key="UserVerified" a:Value="N" />
        <a:KV a:Key="TryAgain" a:Value="Y" />
        <a:KV a:Key="DataSource" a:Value="NA" />
  </UserCheckResponse>

1 个答案:

答案 0 :(得分:2)

我会说使用 XMLSerialize代替 DataContractSerializer(WCF的默认值)。 为您的操作添加“[System.ServiceModel.XmlSerializerFormatAttribute()]”。然后使用XmlElementAttribute作为响应节点。 当我不忙的时候,我会给一些样品。因为我目前正在进行遗留Web服务调用,并使用遗留请求调用WCF服务返回遗留响应。

相关问题