Filtered by Tag (katana)
With Visual Studio 2013 RC, we are introduced to a new extensiblity point: External Host. This gives us the F5 experience Wwth OwinHost.exe on VS 2013 and this post walks you through this feature.
@ 09-09-2013
by Tugberk Ugurlu


Since the first day I started developing software solutions for web on Visual Studio, I have had the 'F5' experience. It's nice, clean and easy to get up a web application running without digging through a specific exe file to invoke. After Cassini, our development environments have change a little with IIS Express, in a nice way of course. With the introduction of IIS Express, running it through the command line is also possible but we still have the ultimate F5 experience to get it up.

Today, we are still in love with IIS but with the introduction of OWIN, it's now much easier to build your application in an hosting agnostic way and Katana Project is providing components for hosting your application outside of IIS with a very thin wrapper on top of HttpListener. Actually, the HttpListener is one of the options in terms of receiving the HTTP request and sending back the response at the operating system level. The OwinHost.exe even abstracts that part for us. The OwinHost.exe is a very simple tool which will bring your application startup and the host layer together. Then, it will get out of your way.

However, the development environment with a custom executable tool can be very frustrating, especially in .NET ecosystem where we have really great tooling all over the place. Typically, if you would like to host your application with OwinHost.exe in your development environment, here is what you need to do:

  1. Create a class library project and install Microsoft.Owin package from MyGet ASP.NET Web Stack Nightlies feed.
  2. Add your startup class and build your pipeline with several OWIN middlewares inside the Configuration method.
  3. Add an assembly level attribute for OwinStartupAttribute to indicate your start-up class.
  4. Optionally, install the OwinHost.exe from the same feed into your project or install it any other place.
  5. Go to project properties window and navigate the "Build" pane. Change the "Output Path" from "bin\Debug\" or "bin\Release\" to "bin".
  6. Open up a command prompt window and navigate the root of your project.
  7. Execute the OwinHost.exe from the command prompt window you have just opened up and here you have the running HTTP application.

First four steps are the ones that we are OK with but the rest is generally frustrating. What we really want there is to have the F5 experience that we are used to. With Visual Studio 2013 RC, we are introduced to a new extensibility point in Visual Studio: External Host. With this feature, we are now able to replace the host layer completely. Let's see how. First, let's create a new ASP.NET Web Application:

image

If you are new to this "One ASP.NET" idea, this window may seem a bit awkward at first but think this as an open buffet of frameworks and modules. After selecting this, we will see the open buffet window:

image

For our sample application, we can continue our merry way with the empty project template without any core references. However, the Visual Studio project will still include System.Web and its related references as shown in the below picture.

image

Including the web.config file, we can remove all of this System.Web related stuff now to run our pure OWIN based application. In some cases, we may need web.config though as OwinHost.exe uses it for some cases such as binding redirects. After removing these junks, we have a project as seen below:

image

As we mentioned before, Visual Studio 2013 RC has the external host extensibility support for web applications and we can see this extensibility point by opening the Properties window of the project.

image

You get the IIS Express and Local IIS out of the box with Visual Studio 2013 RC (and yes, Cassini is dead for God's sake). However, it's really easy to get in there and OwinHost NuGet package has the specific installation scripts to register itself as an external host. To get this feature from OwinHost, we just need to add it into our project through NuGet. Today, OwinHost 2.0.0-rc1 package is available on NuGet.org and when we install the package, we get a warning popup from Visual Studio:

image

When we accept the integration, we can see the OwinHost showing up inside the external servers list.

image

If you select the OwinHost option, we can see the option which we can configure if we need to.

image

All these information is stored inside the project file (csproj if you are on C#):

image

Now, I can build my OWIN pipeline and have it running on top of OwinHost.exe just by pressing F5 inside the Visual Studio 2013. For demonstration purposes, I wrote up the following Startup class:

using Microsoft.Owin;
using Owin;
using OwinHostVsIntegrationSample;
using System.IO;

[assembly: OwinStartup(typeof(Startup))]
namespace OwinHostVsIntegrationSample
{
    public class Startup
    {
        public void Configuration(IAppBuilder app) 
        {
            app.Use(async (ctx, next) => 
            {
                TextWriter output = ctx.Get<TextWriter>("host.TraceOutput");
                output.WriteLine("{0} {1}: {2}", 
                    ctx.Request.Scheme, ctx.Request.Method, ctx.Request.Path);

                await ctx.Response.WriteAsync("Hello world!");
            });
        }
    }
}

When I press CTRL + F5, I have my Web application running without debugging on top of OwinHost.exe:

image

Very nice and elegant way of running your application with a custom server! Besides this, we can certainly run our application in debug mode and debug our OWIN pipeline. If you press F5 to run your web application in debug mode, the VS will warn you if you don't have a Web.config file:

image

Not sure why this is still needed but after selecting the first choice, VS will add a web.config file with the necessary configuration and we can now debug our middlewares:

image

Very neat. This sample I demonstrated is also available on GitHub. Enjoy the goodness :)

OWIN and Katana is best way to build web server indipendent web applications in .NET ecosystem and this post will help you a bit on getting started.
@ 05-04-2013
by Tugberk Ugurlu


As usual, I tweeted about my excitement which finally led me to this blog post:

OWIN and Katana project is the effort moving towards that direction and even through it's at its early stages, the adoption is promising. Katana is a flexible set of components for building and hosting OWIN-based web applications. This is the definition that I pulled from its Codeplex site. The stack includes several so-called OWIN middlewares, server and host implementations which work with OWIN-based web applications. You can literally get your application going within no time without needing any installations on the machine other than .NET itself. The other benefit is that your application is not tied to one web server; you can choose any of the web server and host implementations at any time without needing to recompile your project's code.

To get started with Katana today, the best way is to jump to your Visual Studio and navigate to Tools > Library Package Manager > Package Manager Settings. From there, navigate to Package Sources and add the following sources:

Now, we should be able to see those sources through PMC (Package Manager Console):

image

We performed these actions because latest bits of the Katana and OWIN Hosting project are pushed to MyGet and those packages are what you want to work with for now. As you can guess, those packages are not stable and not meant to be for production use but good for demonstration cases :) Let's start writing some code and see the beauty.

I started by creating an empty C# Class Library project. Before moving forward, I would like to take a step back and see what packages I have. I selected the MyGet Owin as the current package source and executed the following command: Get-Package -ListAvailable -pre

image

These packages are coming from the OWIN Hosting project and I encourage you to check the source code out. Let's do the same for the MyGet Katana source:

image

We got more packages this time and these packages are coming from the Katana project which is hosted on Codeplex. These packages consist of OWIN middlewares, host and server implementations which we will have a chance to use some of them now.

Let's start installing a host implementation: Microsoft.Owin.Host.HttpListener pre-release package. Now, change the current package source selection to MyGet OWIN and install Owin.Extensions package to be able to get the necessary bits and pieces to complete our demo.

The Owin.Extensions package will bring down another package named Owin and that Owin package is the only necessary package to have actually. The others are just there to help us but as you can understand, there is no assembly hell involved when working with OWIN. In fact, the Owin.dll only contains one interface which is IAppBuilder. You may wonder how this thing even works then. The answer is simple actually: by convention and discoverability on pure .NET types. To get a more in depth answer on that question, check out Louis DeJardin's awesome talk on OWIN.

What we need to do now is have a class called Startup and that class will have a method called Configuration which takes an IAppBuilder implementation as a parameter.

public partial class Startup {

    public void Configuration(IAppBuilder app) {

        app.UseHandler(async (request, response) => {

            response.ContentType = "text/html";
            await response.WriteAsync("OWIN Hello World!!");
        });
    }
}

For the demonstration purposes, I used the UseHandler extension method to handle the requests and return the responses. In our case above, all paths will return the same response which is kind of silly but OK for demonstration purposes. To run this application, we need to some sort of a glue which needs to tie our Startup class with the host implementation that we have brought down: Microsoft.Owin.Host.HttpListener. That glue is the OwinHost.exe which we can install from the MyGet Katana NuGet feed.

image

OwinHost.exe is going to prepare the settings to host the application and give them to the hosting implementation. Then, it will get out of the way. To make it run, execute the OwinHost.exe without any arguments under the root of your project and you should see screen as below:

image

We got this unfriendly error message because the OwinHost.exe was unable to locate our assemblies as it looks under the bin directory but our project outputs the compiled assemblies under bin\Debug or bin\Release; depending on the configuration. Change the output directory to bin through the Properties menu, rebuild the solution and run the OwinHost.exe again. This time there should be no error and if we navigate to localhost:5000 (as 5000 is the default port), we should see that the response that we have prepared:

image

Cool! You may wonder how it knows to which host implementation to use. The default behavior is the auto-detect but we can explicitly specify the server type as well (however, it's kind of confusing how to do it today):

image

OwinHost.exe is great but as you can guess, it's not our only option. Thanks to Katana project, we can easily host our application on our own process. This option is particularly useful if you would like to deploy your application as a Windows Service or host your application on Windows Azure Worker Role. To demonstrate this option, I created a console application and referenced our assembly. Then, I installed Microsoft.Owin.Hosting package to be able to host it easily. Here is the code to do that:

class Program {

    static void Main(string[] args) {

        using (IDisposable server = WebApp.Start<Startup>()) {

            Console.WriteLine("Started...");
            Console.ReadKey();
        }
    }
}

When I run the console application now, I can navigate to localhost:5000 again and see my response:

image

I plan on writing a few more posts on OWIN-based applications but for now, that's all I can give away :) The code I demoed here is available on GitHub as well: https://github.com/tugberkugurlu/OwinSamples/tree/master/HelloWorldOwin. but run the Build.cmd file before starting.

Note: Earlier today, I was planning to play with ASP.NET Web API's official OWIN host implementation (which will come out in vNext) and writing a blog post on that. The playing part went well but Flip W. was again a Sergeant Buzzkill :s

 

References



Hi 🙋🏻‍♂️ I'm Tugberk Ugurlu.
Coder 👨🏻‍💻, Speaker 🗣, Author 📚, Microsoft MVP 🕸, Blogger 💻, Software Engineering at Deliveroo 🍕🍜🌯, F1 fan 🏎🚀, Loves travelling 🛫🛬
Lives in Cambridge, UK 🏡