简体   繁体   English

防止添加“ To” SOAP标头

[英]Prevent “To” SOAP header being added

I'm have a problem with a SOAP header created in my C# client. 我在C#客户端中创建的SOAP标头存在问题。 The server is sending back the error 服务器正在发回错误

<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>

I have been under the impression that I have been removing all SOAP headers with the following code. 我一直以为我已使用以下代码删除了所有SOAP标头。

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

However, after activating System.ServiceModel.MessageLogging in the app.config, ( WCF - Inspect the messages being sent/received? ), I see that the server is correct - lo and behold there is a "To" header with "mustUnderstand" set to 1 : 但是,在app.config中激活System.ServiceModel.MessageLogging( WCF-检查正在发送/接收的消息? )之后,我看到服务器是正确的-瞧,那里有一个“ 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>

Any thoughts how I can prevent this header from being added? 有什么想法如何防止添加此标头?

Many thanks. 非常感谢。

In case it helps anybody else, I have found a solution. 万一它对其他人有帮助,我已经找到了解决方案。 In fact Nicolas Giannone provided all the necessary code here WSHttpBinding in .NetStandard or .NET core . 实际上,Nicolas Giannone 在.NetStandard或.NET core中提供了WSHttpBinding所需的所有必需代码。 What we can do is to replace the WSHttpBinding with a custom binding based on the WSHttpBinding, and then replace the TextMessageEncodingBindingElement with one with no addressing. 我们可以做的是用基于WSHttpBinding的自定义绑定替换WSHttpBinding,然后将TextMessageEncodingBindingElement替换为无地址的绑定。 Here's the code : 这是代码:

    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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM