【问题标题】:Adding a custom namespace to soap envelop in WCF在 WCF 中向肥皂信封添加自定义命名空间
【发布时间】:2012-02-29 22:52:07
【问题描述】:

我正在调用一个服务,它需要一个特定的命名空间添加到肥皂包中。

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

    <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,我不确定是否必须在那里做一些事情来添加额外的命名空间。

【问题讨论】:

    标签: c# .net wcf soap


    【解决方案1】:

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

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

    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://weblog.west-wind.com/posts/2016/Apr/02/Custom-Message-Formatting-in-WCF-to-add-all-Namespaces-to-the-SOAP-Envelope

    【讨论】:

      【解决方案2】:

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

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

      这应该允许您在不修改生成的代码的情况下添加命名空间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-25
        • 1970-01-01
        • 2010-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多