Building a Web API 2 project from scratch using OWIN/Katana .NET Part 2: OWIN host
July 6, 2015 8 Comments
Introduction
In the previous post we saw how to build a simple Web API from scratch using a couple of OWIN/Katana components. We added a couple of Katana libraries from NuGet, wired up the routing from the Startup class and we were good to go.
In this post we’ll see one way to make the application run independently of IIS. As we stated before an important benefit of this new OWIN infrastructure is that you can make your ASP.NET web application run on multiple hosts, not only IIS. IIS is only one of the options.
IIS hosting
If you want to host your Web API app on IIS then you don’t need to do anything special as it is the default. System.Web is part of the reference list and the application is set to run on IIS.
OWIN host
It’s easy to change this set up. We started a Web API 2 app called CustomersApi in the previous post. We’ll build this post on that same app.
Let’s start by adding a reference to the following NuGet package:
You’ll notice that this package will add a custom server to the application. The project will only be compatible with Visual Studio 2013 or later:
We’re fine with that so just click Yes. You’ll be presented with a readme file about OwinHost in Visual Studio:
If you go to the project properties you’ll see that OwinHost will be listed among the possible hosting options:
Select that option and save the project. Run the project and you’ll see that the OwinHost.exe file starts up:
By default this starts up localhost:12345. Navigate to the /customers endpoint and you should get the same response as in the previous post:
You can test to remove the System.Web dependency from the project and run the application on OwinHost. It should work like before. The application will continue to work even on IIS in its present state as we don’t use any IIS specific features such as authentication. However, any non-trivial application will still need System.Web if hosted on IIS.
We’ll look at another hosting option in the next post.
View the list of MVC and Web API related posts here.
Reblogged this on Dinesh Ram Kali..
Nice article!!
I am looking for a c++ sdk,, casablanca client communicating with OWIN katana c# server. Canyou guide me ?
Sorry, I can’t help you here. I don’t work with C++ so my knowledge there is near 0.
//Andras
Well, OwinHost doesn’t appear as an option for a server under project properties.
I’m, running VS2013 update 4
Stephen, are you sure you’ve installed the OwinHost package from NuGet? //Andras
Yeah, I followed it step by step.
So, I tried a new project from scratch and the same lack of a drop down entry occurred. In the end I worked through the install.ps1 script contained in the OwinHost package and manually ran the following command and I then got the warning about pre VS2013 not supported, pressed OK and now I see OwinHost in the drop down.
$dte.GetObject(“CustomWebServerProvider”).GetCustomServers(‘MyProjectApi’).AddWebServer(‘OwinHost’, ‘C:\Developer\SrcSt\MyProject\packages\OwinHost.3.0.1\OwinHost.3.0.1\tools\OwinHost.exe’, ‘-u {url}’, ‘http://localhost:12345/’, ‘{projectdir}’)
I’m assuming one of these checks at the start of the script failed and returned:
param($installPath, $toolsPath, $package, $project)
$serverProvider = $dte.GetObject(“CustomWebServerProvider”)
if ($serverProvider -eq $null)
{
return; # Only supported on VS 2013
}
$servers = $serverProvider.GetCustomServers($project.Name)
if ($servers -eq $null)
{
return; # Not a WAP project
}
I’m running Visual Studio Ultimate 2013 Version 12.0.31101.00 Update 4 so I reckon the first return is skipped.
Can you think of any reason why this wouldn’t be a WAP project?
Running
$dte.GetObject(“CustomWebServerProvider”)
gives me
Microsoft.VisualStudio.Web.Application.WebProjectCustomServerProvider
and running
$dte.GetObject(“CustomWebServerProvider”).GetCustomServers(‘MyProjectApi’)
gives
DefaultServerName Count
—————– —–
1
That result doesn’t equal null so I figure the subsequent steps would be executed.
Anyway, I can get there by hand so not all is lost. Would be nice if the packaged script worked though.
Reblogged this on Mavin world and commented:
OWIN explained in a simplified manner.