Create Asynchronous Action Method in Web API

Introduction

This article explains the Asynchronous Action method and how to create it in the Web API. The purpose of this is to create the asynchronous method for handling more than one request on the same number of thread pools. The thread pool is maintained by the .Net framework on the IIS server. There is a finite number of threads available in the thread pool.
When any request arrives the request is assigned to the process by the thread of the thread pool. This thread basically works as the request is processed, and after completing one request the thread is returned back to the pool to service another request. An asynchronous method allows you to start a long-running operation, returns your thread to the pool, and wakes up on a different thread or the same depending on the availability of threads in the pool at that time.

Now create an application.

Create a Web API application as in the following:
  • Start Visual Studio 2012.
  • From the Start window select "New project".
  • Then select "Installed" -> "Visual C#" -> "Web"
  • Select "MVC4 Web Application" and click on the "OK" button.

    Select MVC4 Application
  • From the "MVC4" window select "Web API".

    Select Web API
  • Click on the "OK" button.
Select ApiController "Values Controller'.
  • In the "Solution Explorer".
  • Expand "Controller" folder.
  • Select "ValuesController".

    Select Controller

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Threading;  
  8. using System.Threading.Tasks;  
  9. using System.Web.Http;  
  10. namespace MvcApplication19.Controllers  
  11. {  
  12.     public class ValuesController : ApiController  
  13.     {  
  14.         public async Task<string> Get(int id)  
  15.         {  
  16.             return await ReadFileAsync();  
  17.         }  
  18.         private async Task<string> ReadFileAsync()  
  19.         {  
  20.             using (StreamReader reader = File.OpenText(@"D:\Introduction.txt"))  
  21.             {  
  22.                 await Task.Delay(500);  
  23.                 return await reader.ReadToEndAsync();  
  24.             }  
  25.         }  
  26.     }  
  27. }  
 

In the valueController we modify the code. Here we change the path with the valid text file path that is saved in your system. The following is an explanation of this code:

  • The "ReadFileAsync" method reads the Text file content and returns it to the action method. The action method returns it as the response of this file content.
  • Here we use "await Task.Delay(500);" it defines that simulating the long file for reading.
  • "Task<string>" is used for the return type to the action method.

Then we host the application on the IIS server with some port. Here I host the application with the port "8083" and the entire URL is "http://localhost:8083". Copy the URL.

Host application

To execute the application:

  • Open fiddler.

    Open Fiddler
  • Click on the "Composer tab" in the fiddler.
  • Paste the URL and and change it to "http://localhost:8083/api/values/1".

    Change URL
  • Click n the "Execute" button.
  • Now click on "Inspectors" and click on the "TextView tab".

    Display Content of File
  • It displays the entire text of the file.