In this blog post I want to share with you an hidden feature of Visual Studio 2013 that enables an integrated debugging experience with OwinHost and other custom hosts.

Options when building Owin-based apps

When you build an Owin-based web application with Katana you have 3 hosting options:

  • Use the System.Web Host, build your app as Web Application and run/debug it inside IIS Express from within Visual Studio;
  • Build your own custom host, build your app as Console Application and run/debug it as custom console application;
  • Use the OwinHost host that is part of the Katana suite, and build your app as Class Library and run it by manually launching the OwinHost.exe app from within the current project folder.

How to debug an Owin-based app running within OwinHost

In the third option I left out debug on purpose, it was not a mistake. This is because debugging an Owin app that is running in OwinHost is not as straightforward as the other 2 options:

  • First you have to make sure the project is always built into the \bin folder instead of \bin\Debug or \bin\Release as otherwise OwinHost will not work (by default it looks for classes inside the \bin folder);
  • Then you have to manually launch the OwinHost executable pointing to the folder in which it was installed by Nuget when you downloaded it (typically it would be ..\packages\OwinHost.(version)\tools\OwinHost.exe, and with the just released version, ..\packages\OwinHost.3.0.0\tools\OwinHost.exe)
  • And finally, if you want to debug the application, you have to attach the debugger to the process , and launch the browser manually.

Not a complicate procedure, but time consuming.

Introducing Visual Studio integration for external hosts

Visual Studio 2013 adds the possibility, in addition to the usual IIS Express and Local IIS, to launch a Web Application with a external url or by specifying an external hosting process. And the OwinHost nuget package takes advantage of this and when you install it within a Web Application project it also registers itself as additional custom host (here below the important part of the powershell install script in the nuget package):

$serverProvider = $dte.GetObject("CustomWebServerProvider")
$servers = $serverProvider.GetCustomServers($project.Name)
$servers.AddWebServer('OwinHost', $exeDir, '-u {url}', 'http://localhost:12345/', '{projectdir}')

This adds the following line to the .csproj file:

<servers defaultServer="OwinHost">
  <server name="OwinHost"
   exePath="{solutiondir}\packages\OwinHost.3.0.0\tools\OwinHost.exe"
   cmdArgs="-u {url}"
   url="http://localhost:12345/"
   workingDir="{projectdir}" />
</servers>

In concrete what you get is a new entry in the Servers dropdown list in the Web tab of the Server Properties window of your web application project.

Project Properties Window, Web tab

But since this option is only available inside a Web Application project, you have to create your application using the Empty template of a Web Application project and remove all the unneeded project references that come with Web Application projects, like System.Web for example.

But once the references are cleaned up (and you have to do it only once), you can just hit F5 or press the “Debug” button and OwinHost will fire-up loading your Owin startup class, your browser will start to the right url, and the debugger will be already attached to the process, just like with IIS hosted apps.

Compatibility issues

This new feature only works with Visual Studio 2013, so if you want to open the same project also in VS 2012 do not install it otherwise the project will not load (the Nuget Manager will ask you if you want to install the server extension or not).