title: ServiceStack Messaging API: Using HostContet.AppHost.ExecuteMessage in OnAfterInit gives NullReferenceException
body: <p>As previously <a href="https://stackoverflow.com/questions/64562749/servicestack-reinstate-pipeline-when-invoking-a-service-manually">discussed here</a>, I am sometimes using this approach to execute Services internally:</p>
  <pre><code>IMessage theMessage = new Message&lt;Startup&gt;(new Startup());
  var reply = HostContext.AppHost.ExecuteMessage(theMessage); // The way to get the pipeline to execute when we do internal call, and get DI to work etc
  </code></pre>
  <p>This works well and as expected, except in one case: when I call this from the <code>OnAfterInit</code> method, that I override.</p>
  <p>The idea is that I want to do some startup-stuff when the AppHost is ready, after initialization, but when doing this, it fails with a <code>NullReferenceException</code>:</p>
  <pre><code>public override void OnAfterInit()
  {
      base.OnAfterInit();
  
      IMessage theMessage = new Message&lt;Startup&gt;(new Startup());
      var reply = HostContext.AppHost.ExecuteMessage(theMessage); 
  }
  </code></pre>
  <p>Error:</p>
  <p><a href="https://i.stack.imgur.com/F4nGF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F4nGF.png" alt="enter image description here" /></a></p>
  <pre><code>   at ServiceStack.ServiceStackHost.&lt;ApplyRequestConvertersAsync&gt;d__361.MoveNext() in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\ServiceStackHost.Runtime.cs:line 45
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at ServiceStack.Host.ServiceController.ExecuteMessage(IMessage dto, IRequest req) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ServiceController.cs:line 584
     at ServiceStack.ServiceStackHost.ExecuteMessage(IMessage mqMessage) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\ServiceStackHost.cs:line 1486
     at Test.Test.OnAfterInit() in C:\Git\Test\Test.cs:line 59
  </code></pre>
  <p>It seems that not all is initialized and ready. If I do this, it works:</p>
  <pre><code>public override void OnAfterInit()
  {
      base.OnAfterInit();
      System.Threading.Timer t = new System.Threading.Timer((o) =&gt; 
      {
          IMessage theMessage = new Message&lt;Startup&gt;(new Startup());
          var reply = HostContext.AppHost.ExecuteMessage(theMessage);
      }, null, 100, int.MaxValue);
  }
  </code></pre>
  <p>I would appreciate to know if I am missing something, and if there is a nicer way to resolve this. The Timer-thing works, but it doesn't sit right with me.</p>