Getting started with OWIN

Posted by on Sep 29, 2013 in

OWIN If you’re a developer in the web space, especially focusing or involved with the Microsoft stack, you’ve no doubt heard a lot about OWIN lately. OWIN, or Open Web Interface for .NET is an open-source initiative somewhat inspired by the Rack project on the Ruby platform and the increasingly popular Node.js movement. Like Rack, OWIN sets out to provide a flexible, lightweight middleware between web servers and the underlying framework components. OWIN also delivers on its promise of portability and modularity meaning applications written on top of OWIN are server and potentially even OS agnostic. If, like me, you’ve created a simple web server using Node.js in a dozen lines of code, then self-hosted via a command line, you’ve no doubt been slightly envious as the elegance and autonomy of that solution compared to the tightly coupled MS/IIS/ stack. However, you also soon probably found that a lot of the goodness gives you ‘out of the box’ goes a long way, and with OWIN, developers can potentially have the best of both worlds. Katana If you’re a Microsoft web developer then for every OWIN you’ve probably heard two Katana’s. Katana is Microsofts (also open-source) implementation of the OWIN specification. It can be found on Codeplex and is unsurprisingly geared to provision for the Microsoft hosts and frameworks. An Example Lets put this into an example. Using Visual Studio, click File, New Project and select Console App Once that’s completed, we need to grab a few packages from NuGet. From the package manager console (or via the GUI if you’d prefer), obtain the following packages: install-package Microsoft.Owin.Hosting –IncludePreRelease install-package Microsoft.Owin.Host.HttpListener –IncludePreRelease Once the process completes you’ll notice a few dependencies have been imported for you via the magic of NuGet dependency management: Now your app is good to go. It’s quite basic: C# using Microsoft.Owin.Hosting; using System; namespace Owin.ConsoleApp { class Program { static void Main(string[] args) { string uri = "http://localhost:8001"; using (WebApp.Start<StartUp>(uri)) { Console.WriteLine("App started"); Console.ReadKey(); Console.WriteLine("App stopping..."); } } } } 1234567891011121314151617181920 using Microsoft.Owin.Hosting;using System; namespace Owin.ConsoleApp{    class Program    {        static void Main(string[] args)        {            string uri = "http://localhost:8001";            using (WebApp.Start<StartUp>(uri))            {                Console.WriteLine("App started");                Console.ReadKey();                Console.WriteLine("App stopping...");            }        }    }} The static WebApp class has a generic Start method which takes in a URL on which to listen and a TStartup parameter which is a class that defines our configuration for the application. As per the above, create a new class inside the project called Startup.cs as follows: C# namespace Owin.ConsoleApp { class StartUp { public void Configuration(IAppBuilder app) { app.Run(ctx => { ctx.Response.ContentType = "text/plain"; return ctx.Response.WriteAsync("Helo from OWIN"); }); } } } 12345678910111213141516 namespace Owin.ConsoleApp{    class StartUp    {         public void Configuration(IAppBuilder app)        {            app.Run(ctx =>            {                ctx.Response.ContentType = "text/plain";                return ctx.Response.WriteAsync("Helo from OWIN");            });        }     }} Everything should compile just fine – hit F5 to start the app. A console window should appear telling you the app has started. Next, just open your browser and go to the Uri specified in the app (in this...

Read More »