title: Serialization inside ASP.Net MVC Action
body: <p>Updated question:
  With or without lock statement, the behavior is the same. So likely I am missing some configuration which is making action execution one thread at a time.</p>
  <p>Previous question:
  My understanding is that thread acquires lock only during the scope of execution of statements within brackets of lock { }. But from logs it appears as if the whole method is under lock statement.</p>
  <p>Here is sample code:</p>
  <pre class="lang-cs prettyprint-override"><code>public class HomeController
  {
      public static int VisitCounter { get; set; }
      static object locker = new object();
      public string Index()
      {
          var tid = Thread.CurrentThread.ManagedThreadId;
          System.Console.WriteLine($&quot;Method Enter:{tid} {DateTime.Now}&quot;);
          // lock (locker)  // commented for updated question
          {
              System.Console.WriteLine($&quot;Lock Enter  :{tid} {DateTime.Now}&quot;);
              VisitCounter++;
              Thread.Sleep(5000);
              System.Console.WriteLine($&quot;Lock Exit   :{tid} {DateTime.Now}&quot;);
          }
          System.Console.WriteLine($&quot;Method Exit :{tid} {DateTime.Now}&quot;);
          return $&quot;Hello. You are visitor number: {VisitCounter}.&quot;;
      }
  }
  </code></pre>
  <p>Actual Output:
  Both threads appear serialized at method-level.</p>
  <pre><code>Method Enter:10 3/10/2021 10:00:00 AM
  Lock Enter  :10 3/10/2021 10:00:00 AM
  Lock Exit   :10 3/10/2021 10:00:05 AM
  Method Exit :10 3/10/2021 10:00:05 AM
  Method Enter:11 3/10/2021 10:00:05 AM
  Lock Enter  :11 3/10/2021 10:00:05 AM
  Lock Exit   :11 3/10/2021 10:00:10 AM
  Method Exit :11 3/10/2021 10:00:10 AM
  </code></pre>
  <p>Expected Output:
  Both http requests should arrive at method approx at the same time, but only one should enter locked section of code at a time. Only locked section should be serialized.</p>
  <pre><code>Method Enter:10 3/10/2021 10:00:00 AM
  Method Enter:11 3/10/2021 10:00:02 AM
  Lock Enter  :10 3/10/2021 10:00:00 AM
  Lock Exit   :10 3/10/2021 10:00:05 AM
  Lock Enter  :11 3/10/2021 10:00:05 AM
  Method Exit :10 3/10/2021 10:00:05 AM
  Lock Exit   :11 3/10/2021 10:00:10 AM
  Method Exit :11 3/10/2021 10:00:10 AM
  </code></pre>
  <hr />
  <p>Just for reference, here is Startup.cs</p>
  <pre class="lang-cs prettyprint-override"><code>public class Startup
  {
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddControllersWithViews();
      }
  
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
          app.UseRouting();
          app.UseEndpoints(endpoints =&gt;
          {
              endpoints.MapDefaultControllerRoute();
          });
      }
  }
  </code></pre>
  <hr />
  <p><strong>Steps to recreate:</strong></p>
  <ol>
  <li>dotnet new web</li>
  <li>Place controller code in Controllers folder.</li>
  <li>Update startup code</li>
  <li>Run it: dotnet run</li>
  </ol>