title: WCF Adding Custom Headers and Session
body: <p>I have a web page that uses a WCF service. Multiple users maybe using the web page at any one time and therefore making requests to the WCF service which is on a remote machine.</p>
  
  <p>Each user on the web page gets a unique ID, I want to add this unique ID to the request header of each request made by that user.</p>
  
  <p>So far I have created the following code which correctly adds a header to the WCF message.</p>
  
  <pre><code>public class HeaderIdPusher : IClientMessageInspector
  {
  
      private static readonly string _balancerKey = "balancerId";
      public object BeforeSendRequest(ref Message request, IClientChannel channel)
      {
          Guid userId = Guid.NewGuid();
  
  
          HttpRequestMessageProperty httpRequestMessage;
          object httpRequestMessageObject;
          if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
          {
              httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
              if (string.IsNullOrEmpty(httpRequestMessage.Headers[_balancerKey]))
              {
                  httpRequestMessage.Headers[_balancerKey] = userId.ToString();
              }
          }
          else
          {
              httpRequestMessage = new HttpRequestMessageProperty();
              httpRequestMessage.Headers.Add(_balancerKey, userId.ToString());
              request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
          }
          return null;
  
  
  
      }
  
      public void AfterReceiveReply(ref Message reply, object correlationState)
      {
  
      }
  }
  </code></pre>
  
  <p>However I am no stuck because I can't get the ID to persist between requests. You can see here that at the moment I am generating an ID for each request, however I can't store this in the Session of the page the user is on because the HttpContext.Current is null. Is there another way of storing this? Is there another way of passing in the HttpContext of the user on my web page?</p>