title: Queuing tasks in asp.net core
body: <p>E.g. of functionality There is 20 users and they clicked send button almost in one time, so methods stacking in queue and first user message is sent and response received, after second third and so on. Users wont chat with other people but with device which response is pretty fast<br>
  So I am trying to queue Task which sends Message.<br>
  I found code samples that uses Task queuing as shown in <strong>Example 1</strong> and <strong>Example 2</strong>.
  <br></p>
  
  <p><strong>Example 1</strong></p>
  
  <pre><code>public class SerialQueue
  {
      readonly object _locker = new object();
      WeakReference&lt;Task&gt; _lastTask;
  
      public Task Enqueue(Action action)
      {
          return Enqueue&lt;object&gt;(() =&gt; {
              action();
              return null;
          });
      }
  
      public Task&lt;T&gt; Enqueue&lt;T&gt;(Func&lt;T&gt; function)
      {
          lock (_locker)
          {
              Task lastTask = null;
              Task&lt;T&gt; resultTask = null;
  
              if (_lastTask != null &amp;&amp; _lastTask.TryGetTarget(out lastTask))
              {
                  resultTask = lastTask.ContinueWith(_ =&gt; function());
              }
              else
              {
                  resultTask = Task.Run(function);
              }
  
              _lastTask = new WeakReference&lt;Task&gt;(resultTask);
              return resultTask;
          }
      }
  }
  </code></pre>
  
  <p><br>
  <strong>Example 2</strong></p>
  
  <pre><code> public class TaskQueue
  {
      private readonly SemaphoreSlim _semaphoreSlim;
  
      public TaskQueue()
      {
          _semaphoreSlim = new SemaphoreSlim(1);
      }
  
      public async Task&lt;T&gt; Enqueue&lt;T&gt;(Func&lt;Task&lt;T&gt;&gt; taskGenerator)
      {
          await _semaphoreSlim.WaitAsync();
  
          try
          {
              return await taskGenerator();
          }
          finally
          {
              _semaphoreSlim.Release();
          }
      }
  
      public async Task Enqueue(Func&lt;Task&gt; taskGenerator)
      {
          await _semaphoreSlim.WaitAsync();
          try
          {
              await taskGenerator();
          }
          finally
          {
              _semaphoreSlim.Release();
          }
      }
  }
  </code></pre>
  
  <p><br>
  Problem is that when I'm passing task which I want to queue (<strong>Example 3</strong>) each time I pressing button, tasks still are executed at the same time and interrupting each other.
  <br></p>
  
  <p><strong>Example 3</strong></p>
  
  <pre><code> [HttpPost(Name = "add-message")]
          public async Task&lt;IActionResult&gt; PostMessage([FromBody] MessengerViewModel messengerViewModel)
          {
              TaskQueue taskQueue = new TaskQueue();
              SerialQueue serialQueue = new SerialQueue();
  
              await taskQueue.Enqueue(() =&gt; SendMessage(messengerViewModel.PhoneNr, messengerViewModel.MessageBody,
                  messengerViewModel.ContactId, messengerViewModel.State));
  //I'm not running tasks at same time, using one or other at time
              await serialQueue.Enqueue(() =&gt; SendMessage(messengerViewModel.PhoneNr, messengerViewModel.MessageBody,
                  messengerViewModel.ContactId, messengerViewModel.State));
  
              return Ok();
          }
  </code></pre>
  
  <p>How could I solve problem and stack task to queue by each click?</p>