Asynchronous Controller of Web API 2 With Entity Framework 6: Get() Method

We know that both are the latest release of the Microsoft stack. It will be a nice combination if we combine Entity Framework 6 with the Web API in application development. Anyway this article explains the Get() method to get data from the Web API asynchronously.

Please check whether or not you are using Entity Framework 6 or higher because Entity Framework 6 supports DB operations in Asynchronously.

In our example we will consume data from the .NET client, if necessary you can implement a JavaScript client too. So, let's pick the Web API 2 template and create this simple controller.

The ResponseType attribute will define the response type of the controller. Since we will return a company record from the database (very soon we will see the table). Since the controller is asynchronous in nature, we are returning a Task and we are using the await keyword to fetch data from the Entity Framework.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Http;  
  6. using System.Web.Mvc;  
  7. using System.Threading.Tasks;  
  8. using System.Web.Http.Description;  
  9. namespace WebAPI.Controllers  
  10. {  
  11.     public class AsyncPersonController : ApiController  
  12.     {  
  13.         [ResponseType(typeof(company))]  
  14.         public async Task<IHttpActionResult> Get(int Id)  
  15.         {  
  16.             using (var ctx = new efDBEntities())  
  17.             {  
  18.                 company com = await ctx.company.FindAsync(Id);  
  19.                 if (com != null)  
  20.                     return Ok(com);  
  21.                 else  
  22.                     return NotFound();  
  23.             }  
  24.         }  
  25.     }  
  26. } 

If the company is not null then it will return Ok, a success message from the Controller otherwise it will return NotFound.

Here is our table information from where the Entity Framework will fetch the data.

table

So, if we pass 1 as the id then it will return the first record from the table. Let's create a .NET client to consume data from the Web API. Have a look at the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using ClassLibrary;  
  7. using System.Threading;  
  8. using System.Net.Http;  
  9. using System.Net.Http.Headers;  
  10. using System.Net.Http.Formatting;  
  11. using Newtonsoft.Json;  
  12. namespace ConsoleAPP  
  13. {  
  14.     class Program  
  15.     {  
  16.         static void Main(string[] args)  
  17.         {  
  18.             using (var client = new HttpClient())  
  19.             {  
  20.                 client.BaseAddress = new Uri("http://localhost:1565/");  
  21.                 var response = client.GetAsync("api/AsyncPerson/1").Result;  
  22.                 var data = response.Content;  
  23.                 if (response.IsSuccessStatusCode)  
  24.                 {  
  25.                     Task<string> d = data.ReadAsStringAsync();  
  26.                     Console.Write(d.Result.ToString());  
  27.                 }  
  28.                 Console.ReadLine();  
  29.             }  
  30.         }  
  31.     }  
  32. } 

And it will print the first record from the table.

record from the table

If you want to read all the data from the DB, then the scenario is changed a little, there is not an asynchronous method that can fetch all the data asynchronously. Here is one synchronous solution but since the function is decorated with the “async” keyword, we can call it asynchronously.
  1. public async Task<IHttpActionResult> GetAll()  
  2. {  
  3.     using (var ctx = new efDBEntities())  
  4.     {  
  5.         List<company> com = ctx.company.ToList();  
  6.         if (com != null)  
  7.             return Ok(com);  
  8.         else  
  9.             return NotFound();  
  10.     }  
  11. } 

Here the return string is in JSON format.

JSON format

Finally

This article showed how to read data from the Web API asynchronously. In a future article we will do the Put() , post() and delete operations asynchronously.