0

我正在开发一个使用 SOAP WCF WS-Addressing 消息与遗留系统通信的客户端。

此外,还需要使用包含自定义信息的标头来自定义 SOAP-Envelope 标头ToAction

我能够通过使用如下代码所示的信息To来设置 SOAP-Envelope 标头:ActionOperationContextScope


public async Task<getAttorneyResponseStructure> GetAttorneyAsync(GetAttorneyRequestStructure getAttorneyRequestStructure)
{
  try
  {
    using (new OperationContextScope(Client.InnerChannel))
    {
      getAttorneyRequestStructure.AttorneyHeader = Header;

      OperationContext.Current.OutgoingMessageHeaders.To = new Uri("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc");

      OperationContext.Current.OutgoingMessageHeaders.Action = "http://tempuri.org/IAttorneyInquiryService/GetAttorney";

      return await Client.GetAttorneyAsync(getAttorneyRequestStructure);
    }
  }
  catch (Exception e)
  {
   throw;
  }
}

当我运行代码并尝试发送消息时,我最终遇到了异常Multiple headers with name 'Action' and namespace 'http://schemas.microsoft.com/ws/2005/05/addressing/none' found.

通过查看图片中附加的异常堆栈,似乎有一个对象包含我要添加的标头的相同信息。

在此处输入图像描述

所以,我的问题是有没有改变Action标题的命名空间或修改Action包含集合命名空间的现有工作?

4

1 回答 1

0

解决了!事实证明,我使用的默认问题是使用BasicHttpBinding与服务器不同的不同肥皂版本。此外,Action不需要标头中的属性,因为我CustomBinding在连接构造函数中使用如下更改了 SOAP 版本:

CustomBinding binding = new CustomBinding();

var mtomMessageEncodingBindingElement = new MtomMessageEncodingBindingElement
{
  MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.WSAddressing10),
};
binding.Elements.Add(mtomMessageEncodingBindingElement);

var httpTransportBindingElement = new HttpTransportBindingElement();
binding.Elements.Add(httpTransportBindingElement);
            
Client = new AttorneyInquiryServiceClient(binding, new EndpointAddress("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc"));

对上面代码的一些额外解释:

  • MtomMessageEncodingBindingElement: 用于设置 SOAP 版本和启用 MOTM 类型的响应。

  • HttpTransportBindingElement: 绑定中需要。

  • 在方法调用中,Action被移除。

public async Task<getAttorneyResponseStructure> GetAttorneyAsync(GetAttorneyRequestStructure getAttorneyRequestStructure)
{
  try
  {
    using (new OperationContextScope(Client.InnerChannel))
    {
      getAttorneyRequestStructure.AttorneyHeader = Header;

      OperationContext.Current.OutgoingMessageHeaders.To = new Uri("http://rydwvgsn01.spga.gov.sa/GSBExpress/Legal/MOJAttorneyInquiry/2.0/AttorneyInquiryService.svc");

      return await Client.GetAttorneyAsync(getAttorneyRequestStructure);
    }
  }
  catch (Exception e)
  {
   throw;
  }
}

于 2021-11-21T12:33:21.220 回答