title: WCF Client: Forcing Global Namespaces
body: <p>I'm working on interfacing with a SOAP service that appears to not deal with default namespaces, but works fine with global namespaces and namespace prefixes declared at the SOAP envelope level.</p>
  
  <p>The problem is that WCF doesn't create these global namespaces at the root, but rather uses explicit unprefixed default namespaces which the service apparently is choking on. Now I know that this not really WCF's fault - I believe the WCF generated messages are valid XML, but the service chokes on it nonetheless.</p>
  
  <p>Using WCF the output generated looks like this:</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;s:Header&gt;
      &lt;h:Security xmlns:h="http://docs.oasis-open.org/wss/2004/01/oasis-
            ...
      &lt;/h:Security&gt;
    &lt;/s:Header&gt;
    &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;cancelShipmentRequest xmlns="http://www.royalmailgroup.com/api/ship/V2"&gt;
        &lt;integrationHeader&gt;
          &lt;dateTime xmlns="http://www.royalmailgroup.com/integration/core/V1"&gt;2016-03-26T01:44:37.0493801Z&lt;/dateTime&gt;
          &lt;version xmlns="http://www.royalmailgroup.com/integration/core/V1"&gt;2&lt;/version&gt;
          &lt;identification xmlns="http://www.royalmailgroup.com/integration/core/V1"&gt;
            &lt;applicationId&gt;RMG-API-G-01&lt;/applicationId&gt;
            &lt;transactionId&gt;ozhckwej6sxg&lt;/transactionId&gt;
          &lt;/identification&gt;
        &lt;/integrationHeader&gt;
        &lt;cancelShipments&gt;
          &lt;shipmentNumber&gt;TTT001908905GB&lt;/shipmentNumber&gt;
        &lt;/cancelShipments&gt;
      &lt;/cancelShipmentRequest&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>
  
  <p>which doesn't work. </p>
  
  <p>Using the following SOAP envelope (manually in SoapUI) does work however:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:v2="http://www.royalmailgroup.com/api/ship/V2"
    xmlns:v1="http://www.royalmailgroup.com/integration/core/V1"&gt;
    &lt;soapenv:Header&gt;
      &lt;h:Security xmlns:h="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
         ...
      &lt;/h:Security&gt;
    &lt;/soapenv:Header&gt;
    &lt;soapenv:Body&gt;
      &lt;v2:cancelShipmentRequest&gt;
        &lt;v2:integrationHeader&gt;
          &lt;v1:dateTime&gt;2016-03-02T14:55:00Z&lt;/v1:dateTime&gt;
          &lt;v1:version&gt;2&lt;/v1:version&gt;
          &lt;v1:identification&gt;
            &lt;v1:applicationId&gt;RMG-API-G-01&lt;/v1:applicationId&gt;
            &lt;v1:transactionId&gt;wftdaife96gv&lt;/v1:transactionId&gt;
          &lt;/v1:identification&gt;
        &lt;/v2:integrationHeader&gt;
        &lt;v2:cancelShipments&gt;
          &lt;v2:shipmentNumber&gt;TTT001908905GB&lt;/v2:shipmentNumber&gt;
        &lt;/v2:cancelShipments&gt;
      &lt;/v2:cancelShipmentRequest&gt;
    &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>The difference between the two is that the v1 and v2 namespaces are declared globally at the top of the document and there are no local namespace declarations in the second document.</p>
  
  <p>Maybe I'm missing something but to me the WCF generated XML looks to be valid and represent the same document state in terms of the namespacing.</p>
  
  <p>The only difference I can tell is that the way the namespaces are declared. And although the WCF version seems to be valid and producing the same namespacing, the service complains about invalid namespace references.</p>
  
  <blockquote>
    <p>Failed Schema Validation: Message failed schema validation: Schemas validity error : Element 'xmlns': This element is not expected. Expected is ( {<a href="http://www.royalmailgroup.com/api/ship/V2" rel="noreferrer">http://www.royalmailgroup.com/api/ship/V2</a>}integrationHeader ).</p>
  </blockquote>
  
  <p>The question is, what's the best way to force the WCF to add the namespace references at the top instead of inline? The only way I've found so far is to use a message inspector and explicitly rewrite the message, but if I go through all that I might as well just manually create the messages.</p>
  
  <p>Any ideas what I can try to force WCF to use explicit namespace prefixes without manually rewriting the messages?</p>