4
votes

I have created a simple web application using MVC5 and ASP.NET 4.5. Using that web application as the startup project works just fine when running locally. I'm trying to convert this to an OWIN self-hosted application so I have added a Console program to the project and set it as the startup project in Visual Studio (I'm using 2013 Community).

This is my Program class:

class Program
    {
        static void Main(string[] args)
        {
            using (Microsoft.Owin.Hosting.WebApp.Start<WebApp.Startup>("http://localhost:9000"))
            {
                Console.WriteLine("Press [enter] to quit...");
                Console.ReadLine();
            }
        }
    }

This runs fine and doesn't trigger any error. However when I try to reach that URL, all I get is a blank page which is actually a 404 error page (I don't have any content in the page, but looking at the network trace I see that the response is a 404).

This is the Startup class of WebApp:

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        }
    }

Yes, it's empty because I don't have much to do (there's no authentication or database schema update to trigger). I'm sure I'm missing something obvious but I can't seem to find any article detailing what is required to make OWIN hookup correctly to a MVC5 project.

1

1 Answers

5
votes

You cannot use self hosting OWIN with MVC 5. This is one of the limitations of MVC, currently. MVC 6 completely addresses this issue. MVC 5 has deep dependencies on the System.Web and IIS pipeline, which makes it unsuitable for running self hosted. You'll need to either wait for MVC 6, or use OWIN on top of IIS or IIS Express.

When MVC 6 ships, you'll be able to do something similar to this:

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }
}

Other web platforms, like WebAPI 2+ and SignalR 2, have already made this transition away from the System.Web Dependency, and can run on self-hosted OWIN.