title: Web API and Cancellation Token
body: <p>I'm trying to have a Web API and Cancellation Token working together, but it doesn't seem to play nicely for some reason.</p>
  
  <p>Here is my code for my Web API. It contains cancellation token properties, and methods</p>
  
  <pre><code>public class TokenCancellationApiController : ApiController
  {
      private static CancellationTokenSource cTokenSource = new CancellationTokenSource();
      // Create a cancellation token from CancellationTokenSource
      private static CancellationToken cToken = cTokenSource.Token;
      // Create a task and pass the cancellation token
  
      [HttpGet]
      public string BeginLongProcess()
      {
          string returnMessage = "The running process has finished!";
          try
          {
              LongRunningFunc(cToken, 6);
          }
          catch (OperationCanceledException cancelEx)
          {
              returnMessage = "The running process has been cancelled.";
          }
          finally
          {
              cTokenSource.Dispose();
          }
          return returnMessage;
      }
  
      [HttpGet]
      public string CancelLongProcess()
      {
          // cancelling task
          cTokenSource.Cancel();
          return "Cancellation Requested";
      }
  
      private static void LongRunningFunc(CancellationToken token, int seconds)
      {
          Console.WriteLine("Long running method");
          for (int j = 0; j &lt; seconds; j++)
          {
              Thread.Sleep(1000);
              if (token.IsCancellationRequested)
              {
                  Console.WriteLine("Cancellation observed.");
                  throw new OperationCanceledException(token); // acknowledge cancellation
              }
          }
          Console.WriteLine("Done looping");
      }
  }
  </code></pre>
  
  <p>And I have the below HTML code:</p>
  
  <pre><code>&lt;script&gt;
      function BeginLongProcess()
      {
          alert("Will now send AJAX request to start long 6 second process...");
          $.ajax({
              url: "/api/TokenCancellationApi/BeginLongProcess",
              type: "GET",
              dataType: 'json',
              success: function (result) {
                  alert(result);
              },
              error: function (xhr, status, error) {
                  var err = eval("(" + xhr.responseText + ")");
                  console.error(err.Message)
              }
          });
      }
      function CancelLongProcess() {
          $.ajax({
              url: "/api/TokenCancellationApi/CancelLongProcess",
              type: "GET",
              dataType: 'json',
              success: function (result) {
                  alert(result);
              },
              error: function (xhr, status, error) {
                  var err = eval("(" + xhr.responseText + ")");
                  console.error(err.Message)
              }
          });
      }
  &lt;/script&gt;
  
  &lt;form id="aForm" runat="server"&gt;
      &lt;p&gt;
          &lt;button type="button" onclick="BeginLongProcess()"&gt;Begin Long Process&lt;/button&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;button type="button" onclick="CancelLongProcess()"&gt;Cancel Long Process&lt;/button&gt;
      &lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The Web API methods gets called fine.
  When I click the button to begin the long process, and then hit cancel, I expected it to cancel the long process, and return an alert message telling me it was cancelled.</p>
  
  <p>But that's not the case. Although there was a token cancel request, it doesn't seem to be registering, and the long process keeps on running until it is finished.</p>
  
  <p>Can anyone please tell me why is this not working as I want it to?</p>