防止“to”SOAP头被添加

4

我在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中激活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>

有没有想法如何防止添加此标题?非常感谢。
1个回答

5

如果对其他人有所帮助的话,我已经找到了解决方案。实际上,Nicolas Giannone在这里提供了所有必要的代码。我们可以使用基于WSHttpBinding的自定义绑定,然后将TextMessageEncodingBindingElement替换为一个没有寻址(Addressing)的元素。以下是代码:

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

谢谢,这是我们的 .NET 6 项目中唯一有效的解决方案。 - TurboLion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接