title: WCF Behavior Extension to BizTalk WCF-WebHttp Send Port not populating Date Http Header
body: <p>I have a BizTalk 2013 app (not R2) that needs to send a Json document to an external vendor's RESTful api. The vendor requires three Http headers:</p>
  
  <ol>
  <li><blockquote>
    <p>Content-Type: application/json</p>
  </blockquote></li>
  <li><blockquote>
    <p>Date: <em>in ISO8601 UTC format</em></p>
  </blockquote></li>
  <li><blockquote>
    <p>Authorization: <em>custom auth. using a constructed string that includes the above Date value run through the HMACSHA1 hash</em></p>
  </blockquote></li>
  </ol>
  
  <p>In BizTalk, my outbound (xml) message goes to the Send Port, there is a Custom Pipeline Component that transforms to Json using JSON.Net. So far, so good. To add the headers which are unique per message, I created a WCF Behavior extension that implements IClientInspector and IEndpointBehavior. In BeforeSendRequest(), I get a reference to the HttpRequestMessageProperty for the request. </p>
  
  <p>I can successfully add to the Headers Collection a ContentType header and an Authorization header. I cannot add a Date header - no errors, just no header value when examining with Fiddler.</p>
  
  <p>I read somewhere that Date is a restricted header and for a workaround I could use Reflection to get around it. E.g.</p>
  
  <pre><code>    MethodInfo priMethod = headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic);
      priMethod.Invoke(headers, new[] { "Date", ISODateTimestamp });
  </code></pre>
  
  <p>That didn't work either. I'm really stumped with: 1. Why no Date header at all is on my request? 2. If there was one, how I could manipulate it as I need to give that is "restricted"?</p>
  
  <p>I tried two different options: a WCF behavior extension:</p>
  
  <pre><code>public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel)
    {
        System.Diagnostics.Debug.Print("Entering BeforeSendRequest()");
  
        try
        {
            HttpRequestMessageProperty httpRequest = null;
  
            if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
            {
                httpRequest = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            }
  
            WebHeaderCollection headers = httpRequest.Headers;
  
            headers.Add(HttpRequestHeader.ContentType, "application/json");
            headers.Add(string.Format("{0}:{1}", "Date", _taxwareHelper.ISODateTimestamp));
            headers.Add("tweDate", _taxwareHelper.ISODateTimestamp);
            headers.Add(HttpRequestHeader.Authorization, _taxwareHelper.AuthorizationString);
  </code></pre>
  
  <p>and a Custom Pipeline Component in a Send Pipeline</p>
  
  <pre><code>string httpHeaderValue =
      new StringBuilder()
      .Append("Content-Type: application/json")
      .Append("\n")
      //.Append(string.Format("Date:{0}", taxwareHelper.ISODateTimestamp))
      //.Append("\n")
      .Append(string.Format("Date:{0}", "Fri, 10 Jul 2015 08:12:31 GMT"))
      .Append("\n")
      .Append(string.Format("tweDate:{0}", taxwareHelper.ISODateTimestamp))
      .Append("\n")
      .Append(string.Format("Authorization:{0}", taxwareHelper.AuthorizationString))
      .ToString();
  
  pInMsg.Context.Write("HttpHeaders", "http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties", httpHeaderValue);
  </code></pre>
  
  <p>in either case, I can set the Content-Type, the Authorization and test date - tweDate just to test, but I can not set the actual Date header.</p>