c# - 使用 IClientMessageInspector 从 WCF soap 请求中删除操作节点 mustUnderstand

标签 c# web-services wcf soap

我正在使用我无权访问且无法修改的 WSDL 访问 WCF 服务。对于其中一个请求,远程服务正在终止,因为我们正在发送:

<Action s:mustUnderstand="1"....>

经过广泛搜索,我无法找到解决问题的简单方法。所以, 在典型的消息中:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <retrieveBooking xmlns="http://services.rccl.com/Interfaces/RetrieveBooking">
      <OTA_ReadRQ TransactionActionCode="RetrievePrice" SequenceNmbr="1" Version="1" xmlns="http://www.opentravel.org/OTA/2003/05/alpha">

我想我可以删除这个节点作为消息检查器的一部分:

internal class MyMessageInspector : IClientMessageInspector
{
   public object BeforeSendRequest(ref Message aRequest, IClientChannel aChannel)
   {
        //Get rid of mustUnderstand Action node
        foreach (MessageHeaderInfo headerInfo in aRequest.Headers.UnderstoodHeaders)
        {
            aRequest.Headers.UnderstoodHeaders.Remove(headerInfo);
        }

        return null;
   }
} 

但是,即使在我删除所有元素后 aRequest.Headers.UnderstoodHeaders 为空,我仍然看到在 XML 中发出了 Action 节点。

  1. 我必须做什么才能使这项工作成功?
  2. 我如何到达 消息内容,以便我可以检查第一个节点的名称 body 标签 retrieveBooking 在这种情况下? (我只需要做 这是针对特定消息,而不是所有消息)

最佳答案

最后的答案非常简单。

public object BeforeSendRequest(ref Message aRequest, IClientChannel aChannel)
{
   //For the CabinDetail message the API provider has requested that we REMOVE the XML action node from the header as it causes their end to fail
   //<s:Header>
   //<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" />
   //</s:Header>
   if (aRequest.ToString().Contains("CabinDetail"))
   {
       int headerIndexOfAction = aRequest.Headers.FindHeader("Action", "http://schemas.microsoft.com/ws/2005/05/addressing/none");
       aRequest.Headers.RemoveAt(headerIndexOfAction);
   }

   return null;
}

关于c# - 使用 IClientMessageInspector 从 WCF soap 请求中删除操作节点 mustUnderstand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38909949/

相关文章:

c# - 如果在实例化 DbContext 资源的 using 语句中数据库连接失败,我如何自动抛出 ApplicationException?

c# - 当我将 XNA 游戏窗口的垂直尺寸更改为最小时,它会为 spritebatch 抛出 ObjectDisposedException,为什么?

php - 如何在我的 Android 应用程序中收听互联网连接的变化?

c# - 如何正确序列化自动生成的 WCF 类型

c# - Windows 身份验证后的自定义安全模块

c# - 你能放心地说这个代码是 "unsafe"

c# - 从 Windows C# Canon SDK 与 PTP 或 MTP 捕捉图片

java - 如何向不同的网络服务发送多个异步请求?

java - wsdl 没有服务元素

c# - WCF 请求/响应包大小