title: I'm getting a NullReferenceException inside MessageHeaderManager.GetHeader when receiving a message with NserviceBus host
body: <p>I'm using NServiceBus 3 and when I receive a message I'm getting this error from the receiving endpoint.:</p>
  
  <pre><code>NServiceBus.Unicast.Transport.Transactional.TransactionalTransport [(null)] &lt;(null)&gt; - Failed raising 'transport message received' event for message with ID=97d0f2fa-6227-439b-ad37-a304bd2af000\10428
  System.NullReferenceException: Object reference not set to an instance of an object.
     at NServiceBus.MessageHeaders.MessageHeaderManager.GetHeader(Object message, String key)
     at NServiceBus.MessageHeaders.Bootstrapper.&lt;Run&gt;b__0(Object msg, String key)
     at NServiceBus.ExtensionMethods.GetHeader(Object msg, String key)
     at NServiceBus.ExtensionMethods.GetOriginatingSiteHeader(Object msg)
     at NServiceBus.Gateway.HeaderManagement.GatewayHeaderManager.MutateIncoming(Object message)
     at NServiceBus.Unicast.UnicastBus.&lt;&gt;c__DisplayClass25.&lt;ApplyIncomingMessageMutatorsTo&gt;b__24(IMutateIncomingMessages m)
     at System.Collections.Generic.List`1.ForEach(Action`1 action)
     at NServiceBus.Unicast.UnicastBus.ApplyIncomingMessageMutatorsTo(IBuilder builder, Object originalMessage)
     at NServiceBus.Unicast.UnicastBus.&lt;&gt;c__DisplayClass22.&lt;HandleMessage&gt;b__21(Object msg)
     at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
     at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
     at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
     at NServiceBus.Unicast.UnicastBus.HandleMessage(IBuilder builder, TransportMessage m)
     at NServiceBus.Unicast.UnicastBus.HandleTransportMessage(IBuilder childBuilder, TransportMessage msg)
     at NServiceBus.Unicast.UnicastBus.TransportMessageReceived(Object sender, TransportMessageReceivedEventArgs e)
     at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
     at NServiceBus.Unicast.Transport.Transactional.TransactionalTransport.OnTransportMessageReceived(TransportMessage msg)
  </code></pre>
  
  <p>The message itself looks like this: </p>
  
  <pre><code>public interface ILinkPersonMessage : IBaseMessage
  {
      int PersonId { get; set; }
      List&lt;int&gt; UnitIds { get; set; }
  }
  
  public interface IBaseMessage
  {
      int UserId { get; set; }
  }
  </code></pre>
  
  <p>In the sending class I'm doing this:</p>
  
  <pre><code>var message = bus.CreateInstance&lt;ILinkPersonMessage&gt;();
  message.UserId = currentUser.UserId;
  message.PersonId = personId;
  message.UnitIds = selectedUnits;
  
  bus.Send(message);
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>I'm looking through the NServiceBus code, and this is the method that's throwing the error:</p>
  
  <pre><code>    public string GetHeader(object message, string key)
      {
          if (message == ExtensionMethods.CurrentMessageBeingHandled)
              if (bus.CurrentMessageContext.Headers.ContainsKey(key))
                  return bus.CurrentMessageContext.Headers[key];
              else
                  return null;
  
          if (messageHeaders == null)
              return null;
  
          if (!messageHeaders.ContainsKey(message))
              return null;
  
          if (messageHeaders[message].ContainsKey(key))
              return messageHeaders[message][key];
  
          return null;
      }
  </code></pre>
  
  <p>From looking at that code, this could only be happening if <code>bus.CurrentMessageContext</code> or <code>bus.CurrentMessageContext.Headers</code> are null, or if <code>messageHeaders[message]</code> existed, but contained a null value.  Unfortunately I haven't been able to figure out yet which one of these scenarios is more likely (or even possible)</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>So using QueueExplorer I've pulled the headers out:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;ArrayOfHeaderInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;HeaderInfo&gt;
          &lt;Key&gt;NServiceBus.Version&lt;/Key&gt;
          &lt;Value&gt;3.0.3&lt;/Value&gt;
      &lt;/HeaderInfo&gt;
      &lt;HeaderInfo&gt;
          &lt;Key&gt;NServiceBus.TimeSent&lt;/Key&gt;
          &lt;Value&gt;2012-05-08 06:57:15:691208 Z&lt;/Value&gt;
      &lt;/HeaderInfo&gt;
      &lt;HeaderInfo&gt;
          &lt;Key&gt;NServiceBus.EnclosedMessageTypes&lt;/Key&gt;
          &lt;Value&gt;Namespace.Service.ILinkPersonMessage __impl, NServiceBus.Unicast.Transport__impl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null;Namespace.Service.ILinkPersonMessage, Namespace.Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&lt;/Value&gt;
      &lt;/HeaderInfo&gt;
      &lt;HeaderInfo&gt;
          &lt;Key&gt;WinIdName&lt;/Key&gt;
          &lt;Value&gt;*{myusername}*&lt;/Value&gt;
      &lt;/HeaderInfo&gt;
      &lt;HeaderInfo&gt;
          &lt;Key&gt;CorrId&lt;/Key&gt;
      &lt;/HeaderInfo&gt;
  &lt;/ArrayOfHeaderInfo&gt;
  </code></pre>
  
  <p><strong>Update 3</strong></p>
  
  <p>This is the fluent config that now works:</p>
  
  <pre><code>Configure.With()
  .DefiningEventsAs(t =&gt; t.Namespace != null &amp;&amp; t.Namespace.StartsWith("Events"))
  .DefiningCommandsAs(t =&gt; t.Namespace != null &amp;&amp; t.Namespace.StartsWith("Messages"))
  .CastleWindsorBuilder(container)
  .XmlSerializer()
  .MsmqTransport()
  .IsTransactional(true)
  .IsolationLevel(IsolationLevel.ReadUncommitted)
  .UnicastBus()
  .RavenSagaPersister()
  .RavenSubscriptionStorage()
  .UseRavenTimeoutPersister()
  .RunTimeoutManager()
  .ForInstallationOn&lt;NServiceBus.Installation.Environments.Windows&gt;().Install();
  </code></pre>
  
  <p>(I've trimmed all the namespaces)</p>