title: WCF WSDL Soap Header on all operations
body: <p>By defining an attribute that implements IContactBehavior and IWsdlExportExtension and set that attribute on your service contract, you can easily add <strong>Soap Headers to your wsdl</strong> (see <a href="http://wcfextras.codeplex.com/" rel="nofollow noreferrer">http://wcfextras.codeplex.com/</a> for more information)</p>
  
  <p>But now I need to set a Soap Header contract in the wsdl on all Operationcontracts and this time I cannot set an attribute.</p>
  
  <p>Following code (called from IWsdlExportExtension.ExportEndPoint) doesn't work, but does work when called from the SoapHeaderAttributes (that executes an IWsdlExportExtension.ExportContract)</p>
  
  <pre><code>foreach (OperationDescription operationDescription in context.ContractConversionContext.Contract.Operations)
  {
     AddSoapHeader(operationDescription, "SomeHeaderObject", typeof(SomeHeaderObject), SoapHeaderDirection.InOut);                    
  }
  
  internal static void AddSoapHeader(OperationDescription operationDescription, string name, Type type, SoapHeaderDirection direction)
  {
      MessageHeaderDescription header = GetMessageHeader(name, type);
      bool input = ((direction &amp; SoapHeaderDirection.In) == SoapHeaderDirection.In);
      bool output = ((direction &amp; SoapHeaderDirection.Out) == SoapHeaderDirection.Out);
  
      foreach (MessageDescription msgDescription in operationDescription.Messages)
      {
          if ((msgDescription.Direction == MessageDirection.Input &amp;&amp; input) ||
              (msgDescription.Direction == MessageDirection.Output &amp;&amp; output))
              msgDescription.Headers.Add(header);
      }
  }
  
  internal static MessageHeaderDescription GetMessageHeader(string name, Type type)
  {
      string headerNamespace = SoapHeaderHelper.GetNamespace(type);
      MessageHeaderDescription messageHeaderDescription = new MessageHeaderDescription(name, headerNamespace);
      messageHeaderDescription.Type = type;
      return messageHeaderDescription;
  }
  </code></pre>
  
  <p>Anyone has an idea how to apply this code on all operations (without using attributes) and by doing this, adding the contract of the header to the wsdl ?</p>