Web API Self-Hosting

Self-hosting means running our ASP.NET Web API project on our own Web Server rather than using IIS. Here, we are not building a complete HTTP Web Server either. We’re simply using the functionality.
 

Introduction

 
This blog shows the walkthrough of how to call a Web API from a client application which we have created earlier.
 
Let's start by creating a simple console application to the existing solution which we have created.
 
Step 1
 
Right-click the Solution Explorer and select "Add New Project".
 
Web API Self Hosting 
 
Step 2
 
Install Microsoft.AspNet.WebApi.SelfHost through Packager Manager console, as shown below.
 
Click on Tools--->Select Library Package Manager-->Package Manager Console and type the following command.
 
Web API Self Hosting 
 
nuget package manager is: Install-Package Microsoft.AspNet.WebApi.SelfHost -Version 5.2.7
 
Step 3
 
Now, add a reference in TestClient to the SampleSelfHost project.
 
In Solution Explorer, right-click the ClientApp project and select "Add Reference". In the "Reference Manager" dialog, under Solution, select Projects. Select the SelfHost project. Click "OK".

Web API Self Hosting
 
Step 4
 
Now, open the Program.cs file and add the following implementation. Then, run the project by setting it as a startup project. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Net.Http;  
  4. namespace TestClient {  
  5.     class Program {  
  6.         static HttpClient client = new HttpClient();  
  7.         static void Main(string[] args) {  
  8.             client.BaseAddress = new Uri("http://localhost:8080");  
  9.             ListAllStudents();  
  10.             ListStudent(1);  
  11.             ListStudents("seventh");  
  12.             Console.WriteLine("Press Enter to quit.");  
  13.             Console.ReadLine();  
  14.         }  
  15.         static void ListAllStudents() {  
  16.             //Call HttpClient.GetAsync to send a GET request to the appropriate URI  
  17.             HttpResponseMessage resp = client.GetAsync("api/students").Result;  
  18.             //This method throws an exception if the HTTP response status is an error code.  
  19.             resp.EnsureSuccessStatusCode();  
  20.             var students = resp.Content.ReadAsAsync < IEnumerable < SampleSelfHost.Student >> ().Result;  
  21.             foreach(var p in students) {  
  22.                 Console.WriteLine("{0} {1} {2} ({3})", p.Id, p.Name, p.Standard, p.Marks);  
  23.             }  
  24.         }  
  25.         static void ListStudent(int id) {  
  26.             var resp = client.GetAsync(string.Format("api/students/{0}", id)).Result;  
  27.             resp.EnsureSuccessStatusCode();  
  28.             var student = resp.Content.ReadAsAsync < SampleSelfHost.Student > ().Result;  
  29.             Console.WriteLine("ID {0}: {1}", id, student.Name);  
  30.         }  
  31.         static void ListStudents(string standard) {  
  32.             Console.WriteLine("Students in '{0}':", standard);  
  33.             string query = string.Format("api/students?standard={0}", standard);  
  34.             var resp = client.GetAsync(query).Result;  
  35.             resp.EnsureSuccessStatusCode();  
  36.             //This method is an extension method, defined in System.Net.Http.HttpContentExtensions  
  37.             var students = resp.Content.ReadAsAsync < IEnumerable < SampleSelfHost.Student >> ().Result;  
  38.             foreach(var student in students) {  
  39.                 Console.WriteLine(student.Name);  
  40.             }  
  41.         }  
  42.     }  
  43. }  

Conclusion

 
In this blog, we have learned how to call a Web API from a console application.
Next Recommended Reading How To Enable Proxy For Web API?