繁体   English   中英

在WCF中向SOAP信封添加自定义名称空间

[英]Adding a custom namespace to soap envelop in WCF

我正在调用服务,该服务需要将特定的名称空间添加到soap信封中。

例如,这是我的示例常规肥皂消息

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:sec="ANOTHER NAMESPACE THAT I WANT TO ADD" >
      <s:Header>

     </s:Header>
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xyz xmlns="">
   <customerId>2511</customerId>
  </xyz>
   </s:Body>
   </s:Envelope>

我已经在实现IDispatchMessageInspector,IClientMessageInspector用于其他目的,我不确定是否必须在此处做一些事情来添加额外的命名空间。

您可以将名称空间添加为自定义Message实现的一部分,该实现​​包括一个OnWriteStartEnvelope()方法,您可以重写该方法并将任何自定义名称空间添加到其中。 然后,将消息连接到MessageFormatter ,然后使用MessageFormatAttribute将行为附加到特定方法。

添加名称空间的关键方法在重写的Message实现上,您可以在其中将名称空间添加到Envelope:

protected override void OnWriteStartEnvelope(XmlDictionaryWriter writer)
{
        writer.WriteStartElement("soapenv", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
        writer.WriteAttributeString("xmlns", "oas", null, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        writer.WriteAttributeString("xmlns", "v2", null, "http://www.royalmailgroup.com/api/ship/V2");
        writer.WriteAttributeString("xmlns", "v1", null, "http://www.royalmailgroup.com/integration/core/V1");
        writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
        writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");            
}

一旦附加到信封,文档的其余部分将重新使用这些顶级声明的名称空间,而不是内联名称空间。

我写了一篇博客文章,描述了涉及MessageMessageFormatterFormatMessageAttribute实现的完整过程: http : FormatMessageAttribute 对附加所有的命名空间,以最SOAP信封

如果您是通过svcutil或通过添加外部引用生成代码的,则可以执行以下操作:

[System.ServiceModel.ServiceContractAttribute(Namespace = "ANOTHER NAMESPACE THAT I WANT TO ADD", Name = "sec")]
public partial class TheClassYouAreUsingForAClient {  }

这应该允许您添加名称空间而无需修改生成的代码。

暂无
暂无

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

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