Self Hosting Web API find the best api

Where Can You Find the Best Self Hosting Web API?

Spread the love

In this post, learn about self-hosting and how to Self Hosting Web API the ASP.NET Web API. You won’t need the IIS server for this topic. We use a console program to host the Web API.

Step 1

We start by developing the console application.

  • The launch of Visual Studio 2012.
  • “New Project” can be chosen from the Start page.
  • Choose Installed template->Visual C# from the Templates menu.
  • Change the name to “Hosting” and choose “Console application.”
  • On the “OK” button, click.
developing the console application

Step 2

“.Net Framework 4” should be set.

  • Right-click the project in the “Solution Explorer.”
  • “Properties” should be chosen.
  • Choose “.Net Framework 4” from the Destination Folder Drop Down List in the Properties dialogue.
Net Framework
  • Open a popup to apply the adjustment, then select “Yes.”

Step 3

We determine whether or not the NuGet Package Manager is installed.

  • Go to the Tools menu.
  • The “Nuget Package Manager” is installed if the “Library Package Manager” is visible.
  • It is not installed if “Library Package Manager” is not visible.

For “Nuget Package Manager” installation:

  • Choose “Extension and Updates” from the “Tools” menu.
  • Choose “Online” in the “Extension and Updates” dialogue box.
  • The search term “nuget package management” should be entered.
  • Choose “Install Nuget Package Manager” to do so.

Step 4

The Web API Self-Host Package is now installed.

  • Choose “Library Package Manager” -> “Manages Nuget Packages For Solution” from the “Tools” menu.
  • Type “Microsoft.AspNet.WebApi.SelfHost” in the “Nuget Package Manager” dialogue box.
 Web API Self-Host
  • Click Install after selecting the ASP.NET Web API Standalone Host.
  • shost14.jpg
  • Choose “I Accept” from the menu.

Step 5

Establish the Model class:

  • “Solution Explorer” is where.
  • Choose “Hosting” -> “Add” -> “Class” from the context menu when you right-click the project.
  • Rename the class to “Item” instead.
  • On the “OK” button, click.

Put the following code in the “Item” Class:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Hosting  
  7. {  
  8.     public class Item  
  9.     {  
  10.         public int Id { getset; }  
  11.         public string Name { getset; }  
  12.         public string Category { getset; } 

Step 6

Making a Controller class addition:

  • “Solution Explorer” is where.
  • Choose “Hosting” -> “Add” -> “Class” from the context menu when you right-click the project.
  • Call the class “ItemsController” instead.
  • On the “OK” button, click.
  • In the “ItemsController” class, add the following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Text;  
  6. using System.Web.Http;  
  7. namespace Hosting  
  8. {  
  9.     public class Items Controller : ApiController  
  10.     {  
  11.         Item[] items = new Item[]    
  12.         {   
  13.             new Item { Id = 1, Name = “Apple”, Category = “Fruit” },   
  14.             new Item{ Id = 2, Name = “Tomato”, Category = “vasitable” },   
  15.             new Item{ Id = 3, Name = “T-Shirt”, Category = “cloths” }   
  16.         };  
  17.         public IEnumerable<Item> GetAllItems()  
  18.         {  
  19.             return items;  
  20.         }  
  21.         public Item GetItemById(int id)  
  22.         {  
  23.             var item = items.FirstOrDefault((i) => i.Id == id);  
  24.             if (item == null)  
  25.             {  
  26.                 throw new HttpResponseException(HttpStatusCode.NotFound);  
  27.             }  
  28.             return item;  
  29.         }  
  30.         public IEnumerable<Item> GetItemsByCategory(string category)  
  31.         {  
  32.             return items.Where(i => string. Equals(i.Category, category,  
  33.                     StringComparison.OrdinalIgnoreCase));  
  34.         }  
  35.     }  

Step 7

We now Self Hosting Web API.

  • Open “Program.cs” and enter the following code there:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Web.Http;  
  7. using System.Web.Http.SelfHost;  
  8. namespace Hosting  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             var config = new HttpSelfHostConfiguration(“http://localhost:8080”);  
  15.             config.Routes.MapHttpRoute(  
  16.                 “API Default”, “api/{controller}/{id}”,  
  17.                 new { id = RouteParameter.Optional });  
  18.             using (HttpSelfHostServer server = new HttpSelfHostServer(config))  
  19.             {  
  20.                 server.OpenAsync().Wait();  
  21.                 Console.WriteLine(“Press Enter to quit.”);  
  22.                 Console.ReadLine();  
  23.             }  
  24.         }  
  25.     }  

Step 8

Use the following to add a new project to this application:

  • “Solution Explorer” is where.
  • To start a new project, right-click the project and choose “Add.”
  • Make the name “Client” instead.
Self Hosting Web API

Step 9

With the following, install the “Microsoft ASP.NET Web API Client Library”:

  • Choose “Extension and Updates” from the “Tools” menu.
  • Choose “Online” in the “Extension and Updates” dialogue box.
  • Enter “Microsoft.AspNet.WebApi.Client” in the search box.
  • Installing it involves choosing the “Microsoft ASP.NET Web API library.”

Step 10

Use the following to add the client’s reference to the “Hosting” project:

  • Right-click the “Client” project in the “Solution Explorer.”
  • The “Add reference” option.
  • Launch the “Reference” dialogue window.
  • “Projects” is the answer you should select.

So, choose “Ok” from the menu.

Step 11

  • The “Client” project’s “Program.cs” file should be opened.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Net.Http;  
  7. namespace Client  
  8. {  
  9.     class Program  
  10.     {  
  11.         static HttpClient client = new HttpClient();  
  12.         static void Main(string[] args)  
  13.         {  
  14.             client.BaseAddress = new Uri(“http://localhost:8080”);  
  15.             ListAllItems();  
  16.             ListItem(1);  
  17.             ListItems(“fruit”);  
  18.             Console.WriteLine(“Press Enter to quit.”);  
  19.             Console.ReadLine();  
  20.         }  
  21.         static void ListAllItems()  
  22.         {  
  23.             HttpResponseMessage resp = client.GetAsync(“api/items”).Result;  
  24.             //resp.EnsureSuccessStatusCode();  
  25.             var items = resp.Content.ReadAsAsync<IEnumerable<Hosting.Item>>().Result;  
  26.             foreach (var i in items)  
  27.             {  
  28.                 Console.WriteLine(“{0} {1} {2}”, i.Id, i.Name,  i.Category);  
  29.             }  
  30.         }  
  31.         static void ListItem(int id)  
  32.         {  
  33.             var resp = client.GetAsync(string.Format(“api/products/{0}”, id)).Result;  
  34.             //resp.EnsureSuccessStatusCode();  
  35.             var item = resp.Content.ReadAsAsync<Hosting.Item>().Result;  
  36.             Console.WriteLine(“ID {0}: {1}”, id, item.Name);  
  37.         }  
  38.         static void ListItems(string category)  
  39.         {  
  40.             Console.WriteLine(“items in ‘{0}’:”, category);  
  41.             string query = string. Format(“api/items?category={0}”, category);  
  42.             var resp = client.GetAsync(query).Result;  
  43.             resp.EnsureSuccessStatusCode();  
  44.             var items = resp.Content.ReadAsAsync<IEnumerable<Hosting.Item>>().Result;  
  45.             foreach (var item in items)  
  46.             {  
  47.                 Console.WriteLine(item.Name);  
  48.             }  
  49.         }  
  50.     }  
  51. }   

Step 12

Then Run the application now.

  • Right-click the “Hosting” Project in the “Solution Explorer.”
  • choose “Set as Start Up project” next.
Self Hosting Web API
  • Press F5 for execution.

Step 13

Use the following to debug the hosting project:

  • Right-click the project “Client” in the Solution Explorer.a
  • “Debug” -> “Start new instance” should be selected.

Output

Self Hosting Web API

Leave a Comment