【问题标题】:Prevent "To" SOAP header being added防止添加“To”SOAP 标头
【发布时间】:2018-12-06 09:57:22
【问题描述】:

我的 C# 客户端中创建的 SOAP 标头有问题。服务器正在发回错误

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <s:Header xmlns:s="http://www.w3.org/2003/05/soap-envelope" />
  <soap:Body>
    <soap:Fault>
      <soap:Code>
        <soap:Value>soap:MustUnderstand</soap:Value>
      </soap:Code>
      <soap:Reason>
        <soap:Text xml:lang="en">MustUnderstand headers: [{http://www.w3.org/2005/08/addressing}To] are not understood.</soap:Text>
      </soap:Reason>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

我一直认为我一直在使用以下代码删除所有 SOAP 标头。

internal class CustomMessageInspector : IEndpointBehavior, IClientMessageInspector
{
    public object BeforeSendRequest( ref Message request, IClientChannel channel )
    {
        request.Headers.Clear();
        return null;
    }
    ...
 }

但是,在 app.config (WCF - Inspect the messages being sent/received?) 中激活 System.ServiceModel.MessageLogging 后,我看到服务器是正确的 - 瞧,有一个“to”标头,“mustUnderstand”设置为 1:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:To s:mustUnderstand="1">https://ws-single-transactions-int-bp.nmvs.eu:8443/WS_SINGLE_TRANSACTIONS_V1/SinglePackServiceV30</a:To>
</s:Header>

有什么想法可以防止添加此标头吗?

非常感谢。

【问题讨论】:

    标签: c# soap


    【解决方案1】:

    如果它对其他人有帮助,我已经找到了解决方案。事实上,Nicolas Giannone 在这里提供了所有必要的代码 WSHttpBinding in .NetStandard or .NET core 。我们可以做的是,将 WSHttpBinding 替换为基于 WSHttpBinding 的自定义绑定,然后将 TextMessageEncodingBindingElement 替换为没有寻址的绑定。这是代码:

        string endPoint = myConfig.SinglePackServicesEndPoint;
    
        //Defines a secure binding with certificate authentication
        WSHttpBinding binding = new WSHttpBinding();
        binding.Security.Mode = SecurityMode.Transport;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
    
        // create a new binding based on the existing binding
        var customTransportSecurityBinding = new CustomBinding( binding );
        // locate the TextMessageEncodingBindingElement - that's the party guilty of the inclusion of the "To"
        var ele = customTransportSecurityBinding.Elements.FirstOrDefault( x=>x is TextMessageEncodingBindingElement );
        if( ele != null )
        {
            // and replace it with a version with no addressing
            // replace {Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)}
            //    with {Soap12 (http://www.w3.org/2003/05/soap-envelope) AddressingNone (http://schemas.microsoft.com/ws/2005/05/addressing/none)}
            int index = customTransportSecurityBinding.Elements.IndexOf( ele );
            var textBindingElement = new TextMessageEncodingBindingElement
            {
                MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None)
            };
            customTransportSecurityBinding.Elements[index] = textBindingElement;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 2016-04-10
      • 1970-01-01
      相关资源
      最近更新 更多