Building a Web API 2 project from scratch using OWIN/Katana .NET Part 2: OWIN host

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:

Install OwinHost from NuGet

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:

OwinHost will add custom server to application

We’re fine with that so just click Yes. You’ll be presented with a readme file about OwinHost in Visual Studio:

ReadMe file after installing OwinHost

If you go to the project properties you’ll see that OwinHost will be listed among the possible hosting options:

Change host type to Owin host

Select that option and save the project. Run the project and you’ll see that the OwinHost.exe file starts up:

OwinHost 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:

Customers list presented by OwinHost

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.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

7 Responses to Building a Web API 2 project from scratch using OWIN/Katana .NET Part 2: OWIN host

  1. kru says:

    Nice article!!

    I am looking for a c++ sdk,, casablanca client communicating with OWIN katana c# server. Canyou guide me ?

  2. Stephen York says:

    Well, OwinHost doesn’t appear as an option for a server under project properties.
    I’m, running VS2013 update 4

    • Andras Nemes says:

      Stephen, are you sure you’ve installed the OwinHost package from NuGet? //Andras

      • Stephen York says:

        Yeah, I followed it step by step.

      • Stephen York says:

        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.

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

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: