Get started by using ASP.Net Web API and NancyFx with OWIN/Katana

Many of you may have probably already heard about OWIN (Open Web Interface for .Net) and Katana that implements OWIN and use different components for supporting OWIN, for example System.Web and System.Net.HttpListener. If you haven’t heard about OWIN or Katana before, please read the following: OWIN and Katana.

This blog post will be about using Katana and how to setup the use of ASP.Net Web API and NancyFx.

One way to begin with using Katana is by installing the Katana Tooling. Download the tooling and unzip the downloaded zip file. Then install the Katana.vsix. The file includes Microsoft Visual Studio 2012 templates for Katana Web Applications and Katana Console Applications.

When the installation is completed you can start Visual Studio 2012. Create a New Project and select Templates, Visual C#. You will not see two Katana projects, the Katana Console Application and the Katana Web Application:

The Katana Console Application will use the HttpListener and the Katana Web Application will use System.Web (“ASP.Net”). By using the Katana Web Application, you can use the web.config file, global.asax etc, it is more or less a simple ASP.Net project. The Katana Console Application is more of a lightweight host that don’t take advantage of ASP.Net.

Select the Katana Console Application, name it to what you like. I will use the default suggested name.

The Katana Console Application creates two .cs files, Program.cs and Statup.cs. The Program.cs file looks like this:

[[code]]czoyNzpcIjxzcGFuPmNsYXNzIFByb2dyYW0KPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxNTpcIjxzcGFuPnsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czo0OTpcIjxzcGFuPiAgICBzdGF0aWMgdm9pZCBNYWluKHN0cmluZ1tdIGFyZ3MpCjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoxOTpcIjxzcGFuPiAgICB7Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czo2MTpcIjxzcGFuPiAgICAgICAgc3RyaW5nIHVyaSA9IFwiaHR0cDovL2xvY2FsaG9zdDoxMjM0NS9cIjsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czo2MjpcIjxzcGFuPiAgICAgICAgdXNpbmcgKFdlYkFwcC5TdGFydCZsdDtTdGFydHVwJmd0Oyh1cmkpKQo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czoyMzpcIjxzcGFuPiAgICAgICAgewo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czo1NDpcIjxzcGFuPiAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoXCJTdGFydGVkXCIpOwo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

 

[[code]]czo0NDpcIjxzcGFuPiAgICAgICAgICAgLy8gT3BlbiB0aGUgYnJvd3Nlcgo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czo0NDpcIjxzcGFuPiAgICAgICAgICAgUHJvY2Vzcy5TdGFydCh1cmkpOwo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

 

[[code]]czo2MDpcIjxzcGFuPiAgICAgICAgICAgLy8gUHJlc3MgYW55IGtleSB0byBzdG9wIHRoZSBzZXJ2ZXIKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czo0MzpcIjxzcGFuPiAgICAgICAgICAgQ29uc29sZS5SZWFkS2V5KCk7Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czo1NTpcIjxzcGFuPiAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoXCJTdG9wcGluZ1wiKTsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoyMzpcIjxzcGFuPiAgICAgICAgfQo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czoxOTpcIjxzcGFuPiAgICB9Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoxNTpcIjxzcGFuPn0KPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

 

The WeApp.Start<Statup>(uri) will be used to start listening for incoming request to the specified URI, in this case the http://localhost:12345. The generic Start method will make sure the Startup.cs class will be used during the startup. The Startup class will be used for configuration, more about this later. The Process.Start will simply start a new process, in this case when a URI is specified, your default Internet browser will be opened when the Console application is started.

The Startup.cs file is used for configuration, for example specify which middleware that should be used. A Middleware is the modules that an OWIN host will use, so instead of a normal IIS pipeline with several modules chained after each other, you can add the modules you prefer to use instead. It gives you more freedom to just add those you really needs. Here is the Startup.cs file created by the Katana Console Application template:

[[code]]czo0NjpcIjxicj48c3Bhbj5wdWJsaWMgcGFydGlhbCBjbGFzcyBTdGFydHVwCjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoxNTpcIjxzcGFuPnsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czo2MDpcIjxzcGFuPiAgICBwdWJsaWMgdm9pZCBDb25maWd1cmF0aW9uKElBcHBCdWlsZGVyIGFwcCkKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxOTpcIjxzcGFuPiAgICB7Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoyMzpcIjxzcGFuPiNpZiBERUJVRwo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czo0MDpcIjxzcGFuPiAgICAgICBhcHAuVXNlRXJyb3JQYWdlKCk7Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoyMDpcIjxzcGFuPiNlbmRpZgo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czo0NTpcIjxzcGFuPiAgICAgICBhcHAuVXNlV2VsY29tZVBhZ2UoXCIvXCIpOwo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czoxOTpcIjxzcGFuPiAgICB9Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoxODpcIjxzcGFuPn08L3NwYW4+PGJyPlwiO3tbJiomXX0=[[/code]]

The Configuration method takes an IAppBuilder as an argument. The IAppBuilder is used to register OWIN middlewares. The UseErrorPage and UseWelcomePage methods are extension methods to the IAppBuilder, they are used to add the use of an error page and simply shows a welcome page when the application is started. It’s a common pattern to add an extension method to the IAppBuilder. You can remove the lines inside of the Configuration method because we aren’t going to use them.

Using ASP.Net Web API with OWIN/Katana

 

To use ASP.Net Web API together with OWIN/Katana you need to install ASP.Net Web API OWIN NuGet package. Enter:

[[code]]czo3NjpcIjxicj48c3Bhbj5JbnN0YWxsLVBhY2thZ2UgTWljcm9zb2Z0LkFzcE5ldC5XZWJBcGkuT3dpbiAmbmRhc2g7cHJlPC9zcGF7WyYqJl19bj48YnI+XCI7e1smKiZdfQ==[[/code]]

In the Package Manager Console in Visual Studio 2012 and then press enter to install the ASP.Net Web API.

Note: You can instead of downloading Katana, simply create an empty Console Application and in the Package Manager Console write: Install-Package Microsoft.AspNet.WebApi.OwinSelfHost –Pre, you can read more about it here.

The next step is to configure the use of ASP.Net Web API. In the Configuration method of the Startup.cs file add the following lines of code:

[[code]]czo1MTpcIjxzcGFuPnZhciBjb25maWcgPSBuZXcgSHR0cENvbmZpZ3VyYXRpb24oKTsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

 

[[code]]czo0MTpcIjxzcGFuPmNvbmZpZy5Sb3V0ZXMuTWFwSHR0cFJvdXRlKAo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czo0NjpcIjxzcGFuPiAgICAgICAgICAgICBuYW1lOiBcIkRlZmF1bHRBcGlcIiwKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czo2NjpcIjxzcGFuPiAgICAgICAgICAgICByb3V0ZVRlbXBsYXRlOiBcImFwaS97Y29udHJvbGxlcn0ve2lkfVwiLAo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czo3MzpcIjxzcGFuPiAgICAgICAgICAgICBkZWZhdWx0czogbmV3IHsgaWQgPSBSb3V0ZVBhcmFtZXRlci5PcHRpb25hbCB9Cjwvc3B7WyYqJl19YW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czoyNTpcIjxzcGFuPiAgICAgICAgICk7Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

 

[[code]]czozNzpcIjxzcGFuPmFwcC5Vc2VXZWJBcGkoY29uZmlnKTsgCjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

 

The HttpConfiguration class is used to setup the ASP.Net Web API routes. To use the ASP.Net Web API the IAppBuilder extension method UseWebApi is used. The UseWebApi takes HttpConfiguration as an argument. This is all you need to do to set up the use of the ASP.Net Web API. The next step is to add a simple ApiController.

Add a new class, DefaultController, make sure it inherits the System.Web.Http.ApiController class.

[[code]]czo2MDpcIjxzcGFuPnB1YmxpYyBjbGFzcyBEZWZhdWx0Q29udHJvbGxlciA6IEFwaUNvbnRyb2xsZXIKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxNTpcIjxzcGFuPnsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxNTpcIjxzcGFuPn0KPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

Add a Get method that simply returns “Hello World”.

[[code]]czo2MDpcIjxzcGFuPnB1YmxpYyBjbGFzcyBEZWZhdWx0Q29udHJvbGxlciA6IEFwaUNvbnRyb2xsZXIKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxNTpcIjxzcGFuPnsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czozNzpcIjxzcGFuPiAgICBwdWJsaWMgc3RyaW5nIEdldCgpCjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoxOTpcIjxzcGFuPiAgICB7Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czo0MzpcIjxzcGFuPiAgICAgICByZXR1cm4gXCJIZWxsbyBXb3JsZCFcIjsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxOTpcIjxzcGFuPiAgICB9Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoxNTpcIjxzcGFuPn0KPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

Run the application and in your browser enter: http://localhost:12345/api/Default and you should now get a XML result, something like this:

[[code]]czoxMTU6XCI8c3Bhbj4mbHQ7c3RyaW5nIHhtbG5zPVwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS8yMDAzLzEwL1NlcmlhbGl6e1smKiZdfWF0aW9uL1wiJmd0O0hlbGxvIFdvcmxkISZsdDsvc3RyaW5nJmd0Owo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

You have now created a simple OWIN Host using Katana to host a simple ASP.Net Web API application.

Using NancyFx with OWIN/Katana

 

To use NancyFx you need to install the install the following NuGet package:

[[code]]czo0MzpcIjxzcGFuPkluc3RhbGwtUGFja2FnZSBOYW5jeS5Pd2luPC9zcGFuPjxicj5cIjt7WyYqJl19[[/code]]

Open the Startup.cs file and change the Configuration method to the following code:

 

[[code]]czo1NjpcIjxzcGFuPnB1YmxpYyB2b2lkIENvbmZpZ3VyYXRpb24oSUFwcEJ1aWxkZXIgYXBwKQo8L3NwYW4+XCI7e1smKiZdfQ==[[/code]]

[[code]]czoxNTpcIjxzcGFuPnsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czozMzpcIjxzcGFuPiAgICBhcHAuVXNlTmFuY3koKTsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxNTpcIjxzcGFuPn0KPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

 

This is all you need to specify the use of NancyFx, no need to register routes etc. like you did when using the ASP.Net Web API. The next thing to do is to add a Get “action” method. Create a new class and make sure it inherits the Nancy.NancyModule class:

 

[[code]]czo1NDpcIjxzcGFuPnB1YmxpYyBjbGFzcyBEZWZhdWx0TW9kdWxlIDogTmFuY3lNb2R1bGUKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxNTpcIjxzcGFuPnsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxODpcIjxzcGFuPn08L3NwYW4+PGJyPlwiO3tbJiomXX0=[[/code]]

 

Add a constructor and then use the Get property to specify a route and the lambda expression that should run when the route is accessed by a HTTP GET method, for example returning “Hello World!”:

[[code]]czo1ODpcIjxzcGFuPjxicj5wdWJsaWMgY2xhc3MgRGVmYXVsdE1vZHVsZSA6IE5hbmN5TW9kdWxlCjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoxNTpcIjxzcGFuPnsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czozOTpcIjxzcGFuPiAgIHB1YmxpYyBEZWZhdWx0TW9kdWxlKCkKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxODpcIjxzcGFuPiAgIHsKPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czo1NDpcIjxzcGFuPiAgICAgIEdldFtcIi9cIl0gPSBfID0mZ3Q7IFwiSGVsbG8gV29ybGQhXCI7Cjwvc3Bhbj5cIjt7WyYqJl19[[/code]]

[[code]]czoxODpcIjxzcGFuPiAgIH0KPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

[[code]]czoxNTpcIjxzcGFuPn0KPC9zcGFuPlwiO3tbJiomXX0=[[/code]]

 

Run the application and in your browser enter: http://localhost:12345/ and you should now see “Hello World!” in the browser.

You have now created a simple OWIN Host using Katana to host a simple NancyFx application.

Summary

 

In this blog post you have learned about how easy it is to create a lightweight host to host either ASP.Net Web API or NancyFx by using OWIN and Katana.

If you want to know when I post a blog post, please feel free to follow me on twitter: @fredrikn

Read More

Bok: The Phoenix Project

The Phoenix Project: A Novel About IT, DevOps, and Helping Your Business Win Jag har läst The Phoenix Project i sommar. Jag läser långsamt och det tog mig 10 timmar att läsa ungefär (enligt Amazon). Det är en kort berättelse om den förändring vår bransch går igenom och tar upp waterfail, lite kanban, mycket lite […]

Read More