title: Is a non-blocking, single-threaded, asynchronous web server (like Node.js) possible in .NET?
body: <p>I was looking at <a href="https://stackoverflow.com/q/2842264/541686">this question</a>, looking for a way to create a <em>single-threaded</em>, event-based nonblocking asynchronous web server in .NET.</p>
  
  <p><a href="https://stackoverflow.com/a/2842309/541686">This answer</a> looked promising at first, by claiming that the body of the code runs in a single thread.</p>
  
  <p>However, I tested this in C#:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Threading;
  
  class Program
  {
      static void Main()
      {
          Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
  
          var sc = new SynchronizationContext();
          SynchronizationContext.SetSynchronizationContext(sc);
          {
              var path = Environment.ExpandEnvironmentVariables(
                  @"%SystemRoot%\Notepad.exe");
              var fs = new FileStream(path, FileMode.Open,
                  FileAccess.Read, FileShare.ReadWrite, 1024 * 4, true);
              var bytes = new byte[1024];
              fs.BeginRead(bytes, 0, bytes.Length, ar =&gt;
              {
                  sc.Post(dummy =&gt;
                  {
                      var res = fs.EndRead(ar);
  
                      // Are we in the same thread?
                      Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                  }, null);
              }, null);
          }
          Thread.Sleep(100);
      }
  }
  </code></pre>
  
  <p>And the result was:</p>
  
  <blockquote>
    <p>1<br>
    5</p>
  </blockquote>
  
  <p>So it seems like, contrary to the answer, the thread initiating the read and the thread ending the read are <em>not</em> the same.</p>
  
  <p>So now my question is, how do you to achieve a <em>single-threaded</em>, event-based nonblocking asynchronous web server in .NET?</p>