Thanks for sharing your feedback! If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish - or longer if the blogger is moderating comments.
So #AspNetWebAPI has finally reach beta and I finally have gotten off my a$$ and starting playing around with it. Sure I have read the info on line, I have sat through numerous sessions where it was being taught and all along I have been telling myself ‘this stuff kicks ass’ but I just not have not found the time to actually use it or learn it. That all changed today, and in typically fashion Glenn Block and team have delivered a kick ass, easy as pie solution to a tough issue. They have managed to take WCF and all of its glory and complication and made it dead simple to use.
This is going to be the first of 2 posts where I will walk you though step by step on how to setup and use the AspNetWebApi in a self hosted situation . This post will be how to self host the routes/services and the second post will be how to consume the services from a external (non-MVC) application.
Step 1: Create a windows console application
The simplest way to show how to do this is to create a console app, but of course you could have created a winform, wpf or Windows Service but console apps work just great.
One thing to not though is you will want to open up the Properties for your console app and make sure it is using the Full .net 4 framework, not the client profile. See blow:

Step 2: Getting the bits from Nuget to allow self hosting
The simplest way to get and register the bits needed is to fire up Nuget and download them. If you have not already installed the Nuget bits head over to Nuget.org and download them.
Once you Nuget up and running type in ‘Install-Package' AspNetWebApi.Selfhost’ as shown below.

When this is done running you should have the following assemblies added to your solution:

Step 3: Setting up a basic route and Setup Self Hosting
In order for your application to be able to host WebApi you will need to setup a basic route and the Self hosting Server. You will want to open up your Program.cs file and add the code below.
static void Main(string[] args)
{
var selfHostConfiguraiton = new HttpSelfHostConfiguration( "http://localhost:8080" );
selfHostConfiguraiton.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "endpoints/{controller}",
defaults: null
);
using (var server = new HttpSelfHostServer(selfHostConfiguraiton))
{
server.OpenAsync().Wait();
Console.WriteLine("Hosting at http://localhost:8080/endpoints/{controller}");
Console.ReadLine();
}
}
Breaking down the code above.
- The first line is where you setup the configuration for the WebApiSelfHost Server. When setting this up you will need to make sure to put in a valid URL w/ port, if needed. You may need to use Netsh.exe to register the port if the port you are using is not already setup.
- We are creating our default route. If you have done any Asp.net MVC work this should be confortable for you. The one difference here is we are using MapHttpRoute rather than MapRoute. The MapHttproute can be found in System.web.Http namespace
- Lastly we are creating and starting our Self host Server. This is the magic goo.
Step 4: Creating your first controller
Like Asp.net MVC WebApi users a controller to handle inbound requests. However, we now use ApiController as our base class. The code below is my basic controller for handing the route I defined above.
public class EpisodeController : ApiController
{
public IList GetAllEpisodes()
{
return new List
{
new Episode {Id = 1, Name = "Episode 1", ReleasedOn = DateTime.Now.AddDays( -10 )},
new Episode {Id = 2, Name = "Episode 2", ReleasedOn = DateTime.Now.AddDays( -5 )},
new Episode {Id = 3, Name = "Episode 3", ReleasedOn = DateTime.Now.AddDays( -3 )},
new Episode {Id = 4, Name = "Episode 4", ReleasedOn = DateTime.Now.AddDays( 0 )},
};
}
}
Breaking down the code above.
- We must inherit from ApiController
- If you have done ANY WCF work you will notice that there are ServiceContrace or OperationContract attributes anyplace to be found on the controller….
Step 5: Creating the model to be used by your controller
So we have our controller, now it is time to create the Episode model which is being used by our controller.
public class Episode
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime ReleasedOn { get; set; }
}
Above is our model and if you have done any type of work with WCF you should notice straight away there is NO attributes. This is awesome 
Step 6: Hit F5 and take WebApi For a spin
So we have created the SelfHosting service, we have created our route, we have created our controller and finally we have created out model. It appears to me we are ready to give this puppy a test ride.
Hit F5 in your project and open up a browser. Once your browser is up and running navigate to http://localhost:8080/endpoints/episode.
If all goes well you should see something like

You may notice this is use XML formatting by default. If you don’t want xml follow Glenn’s post on how to disable XML formatting.
Till next time,
Posted
02-27-2012 4:42 AM
by
Derik Whittaker