title: Azure Worker Role: SynchronizationContext.Current is null at first but then changes to class of type System.Threading.SynchronizationContext
body: <p>I have some code that looks like this:</p>
  
  <pre><code>if (SynchronizationContext.Current == null)
  {
      var task = fooAsync();
      task.Wait();
  }
  </code></pre>
  
  <p>I know that Wait() blocks the current thread so it should only be used in Console/Windows Service applications. I thought that in console/windows service SynchronizationContext.Current was always null.</p>
  
  <p>I use this code in Azure Worker Role and at first it executed fine. SynchronizationContext.Current is null.  But at some point it stopped executing.</p>
  
  <p>When I checked the log, SynchronizationContext.Current changed from null to an object of type "System.Threading.SynchronizationContext" and stayed that way.</p>
  
  <p>Microsoft has an article about SynchronizationContext:
  <a href="https://msdn.microsoft.com/magazine/gg598924.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/magazine/gg598924.aspx</a></p>
  
  <p>They say when Current is null, it will use System.Threading.SynchronizationContext by default, but it is weird why it stays null for a while but then starts returning initilized System.Threading.SynchronizationContext.</p>
  
  <p>So why is SynchronizationContext.Current null at first, but then changes to object of type "System.Threading.SynchronizationContext"?  Does it point to some memory leak or is it expected behavior?</p>