title: Add Request header to WCF when using ConfigurationChannelFactory.CreateChannel
body: <p>I need to add a Request Header to a WCF Request when using ConfigurationChannelFactory.CreateChannel.</p>
  
  <p>I have already tried using OperationContextScope.</p>
  
  <p>I have a function which is as shown below:</p>
  
  <pre><code>    public O Execute&lt;O&gt;(Func&lt;T, O&gt; action, string configFilePath, string endpoint, StringDictionary headers)
      {
          bool closed = false;
          T channel = default(T);
          O output = default(O);
  
          try
          {
              channel = this.GetChannel(configFilePath, endpoint);
  
              if (headers != null &amp;&amp; headers.Count &gt; 0)
              {
                  (channel as IClientChannel).Open();
                  using (new OperationContextScope(channel as IClientChannel))
                  {
                      HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                      foreach (DictionaryEntry header in headers)
                      {
                          requestMessage.Headers[header.Key.ToString()] = header.Value.ToString();
                      }
  
                      OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                      output = action(channel);
                  }
                  (channel as IClientChannel).Close();
              }
              else
              {
                  (channel as IClientChannel).Open();
                  output = action(channel);
                  (channel as IClientChannel).Close();
              }
  
              closed = true;
          }
          finally
          {
              if (!closed &amp;&amp; channel != null)
              {
                  (channel as IClientChannel).Abort();
              }
          }
  
          return output;
      }
  
      private T GetChannel(string configFilePath, string endpoint)
      {
          //Get the ChannelFactoryObject
          ConfigurationChannelFactory&lt;T&gt; wcfClientFactory = null;
          ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
          wcfClientFactory = new ConfigurationChannelFactory&lt;T&gt;(endpoint, ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None), null); 
          return wcfClientFactory.CreateChannel();
      }
  </code></pre>
  
  <p>Configuration file entry:</p>
  
  <pre><code>&amp;lt;security mode="Transport"&amp;gt;
     &amp;lt;transport clientCredentialType="None" proxyCredentialType="None" realm="" /&amp;gt;;clientCredentialType="Windows" negotiateServiceCredential="true" /&amp;gt;
  &amp;lt;/security&amp;gt;
  </code></pre>
  
  <p>The above function is called from another .cs file, as shown below, passing <code>Func&lt;T,O&gt;</code> as an argument:</p>
  
  <pre><code>Execute&amp;lt;MyService.InformationResponse[]&amp;gt;=&amp;gt;IMyService.GetInformation(Request), ConfigPath, myServiceEndPoint, headers);
  </code></pre>
  
  <p>I am getting 400, BadRequest as the Service is expecting "Authorization" in the Request header, which it is not able to find.</p>