How to Self-Host ASP.Net Web API: Part 3 of Many

Creating First HTTP Service using ASP.NET Web API: Part1 of Many
Consuming ASP.NET Web API Service using HttpClient: Part2 of Many

In this post, we will step by step walkthrough that How to Self-Host ASP.Net Web API. We are going to host Web API in a console application. To do that creates a Console Application.

image

After creating Console Application make sure to change target framework to .NET Framework 4.0. To change framework right click on the project and select Properties and choose .NET Framework 4 from drop down

image

Next we need to add reference of ASP.Net Web API. We will add reference using NuGet. We will add NuGet Web API package. To add this right click on the project and click on Manage NuGet Package

image

In the search box type Microsoft.AspNet.WebApi.SelfHost and click on search button. Make sure to install Microsoft ASP.NET Web API Self Host in the project.

image

Accept the license term to install the package.

image

By this time we have set up the environment for self-hosting of Web API. Next we will add a Model class. To add that right click and add a class to the project.

Bloggers.cs


namespace webapihostapp
{
public class Bloggers
{
public string Id { get; set; }
public string Name { get; set; }
public string AreaOfIntrest { get; set; }
}
}

Next we will add a Controller class. To add that right click and add a class to the project. Make sure that you are appending Controller with the class name. For example you want to give controller name as abc then make sure that you are giving class name ABController. In this case we are giving controller name BloggersController .

image

Controller class needs to be inherited from ApiController class.

image

Next we need to write Action in the controller. We are writing a simple Action and this is returning List of Bloggers. Controller class will look like following

BloggersController.cs


using System.Collections.Generic;
using System.Web.Http;

namespace webapihostapp
{
public class BloggersController :ApiController
{
public List<Bloggers> GetBloggers()
{
return new List<Bloggers>
{
new Bloggers { Id="1", AreaOfIntrest ="Sql Server " , Name ="Pinal Dave"},
new Bloggers { Id="2", AreaOfIntrest ="ASP.Net " , Name =" Suprotim Agarwal " },
new Bloggers { Id="3", AreaOfIntrest ="C Sharp " , Name ="ShivPrasad Koirala"},
new Bloggers { Id="4", AreaOfIntrest ="Sql Server" , Name =" vinod Kumar " },
new Bloggers { Id="5", AreaOfIntrest ="JavaScript " , Name ="John Papa"},
new Bloggers { Id="6", AreaOfIntrest ="Dan Wahlin " , Name ="HTML5" },
new Bloggers { Id="7", AreaOfIntrest ="Business Intelligence " , Name ="Stephen Forte"},
new Bloggers { Id="8", AreaOfIntrest ="Web API " , Name ="Glen Block" },
new Bloggers { Id="9", AreaOfIntrest ="Windows Azure " , Name ="Gaurav Mantri"},
new Bloggers { Id="10", AreaOfIntrest ="Entity Framework" , Name ="Julie Lerman " },
new Bloggers { Id="11", AreaOfIntrest ="HTML" , Name ="John Bristow"},
new Bloggers { Id="12", AreaOfIntrest ="Silverlight" , Name ="Kunal" },

};

}
}
}

As of now we have Model and Controller in place. We will write code in Program file to host the Web API. Very first add the following namespaces in Program.cs

image

Then create instance of HttpSelfHostConfiguration

image

Next add a default route

image

After adding default route we need to create instance of HttpSelfHostServer and pass the configuration we just created as parameter.

image

Consolidating all the discussion together code to self-host ASP.Net Web API would be as following

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;
using System.Web.Http.SelfHost;

namespace webapihostapp
{
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:9999");
config.Routes.MapHttpRoute(
"API Default", "api/{controller}");

using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
}
}

Now press F5 to run the application

image

Now browse to URL and should able to download Bloggers detail as JSON.

image

In this way we can self-host ASP.Net Web API in a console application. I hope you find this post useful. Thanks for reading.

About these ads

3 thoughts on “How to Self-Host ASP.Net Web API: Part 3 of Many

  1. Pingback: ASP.Net Web API Service in Windows Phone: Part 4 of Many « debug mode……

  2. Pingback: Dew Drop – October 8, 2012 (#1,417) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s