ASP.NET 5 / MVC 6 Console Hosted App

...I had a console application that would use Microsoft.Owin.Hosting.WebApp.Start(...) to host [and to] pass parameters to the initialization method that were determined at runtime...

In ASP.NET 4.x we self-host within a console application using an OWIN host. We run our MyApp.exe directly. Its Main() method calls WebApp.Start() to create the OWIN host. We use an instance of an IAppBuilder to build up the HTTP pipeline via appBuilder.Use() and chain it all together with appBuilder.Build(). This is all within the Microsoft.Owin.Hosting namespace.

Is there a way I can self-host from within a C# console application so I can keep this initialization architecture?

In ASP.NET Core rc2 we self-host inside a console application using an IWebHost. (This is not an OWIN host though OWIN inspired it.) We run our MyApp.exe directly. The Main() method creates a new WebHostBuilder(), which we use to build up the HTTP pipeline via webHostBuilder.Use(), chaining it all together with webHostBuilder.Build(). This is all within the Microsoft.AspNet.Hosting namespace.

Regarding Pinpoint's answer, in ASP.NET Core rc1 we need to run dnx.exe instead of running our app directly. The work of the WebHostBuilder is hidden inside the dnx.exe executable. Dnx.exe also starts-up our application. Our application's Main() method calls WebApplication.Run(), after which we use an instance of IApplicationBuilder to add middleware to the HTTP pipeline via calls to appBuilder.Use(). Both our application and dnx.exe shared the responsibility of creating/configuring the host. It's convoluted and I am glad that this changed in rc2. I supposed that in rc1 the equivalent of OWIN's WebApp.Start() is WebApplication.Run().

ASP.NET 4.x            ASP.NET Core rc1           ASP.NET Core rc2

N/A                    Dnx.exe                      N/A
MyApp.exe              MyApp.dll                    MyApp.exe
Main(args)             Main(args)                   Main(args)
WebApp.Start()         WebApplication.Run(args)     N/A   
appBuilder.Use()       appBuilder.Use()             webHostBuilder.Use()
appBuilder.Build()     N/A                          webHostBuilder.Build()

Some References

Katana's WebApp static class has been replaced by WebHostBuilder, that offers a much more flexible approach:

You've probably already used this API without realizing it, as it's the component used by the hosting block when you register a new web command in your project.json (e.g Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:54540) and run it using dnx (e.g dnx . web):

namespace Microsoft.AspNet.Hosting
    public class Program
        private const string HostingIniFile = "Microsoft.AspNet.Hosting.ini";
        private const string ConfigFileKey = "config";

        private readonly IServiceProvider _serviceProvider;

        public Program(IServiceProvider serviceProvider)
            _serviceProvider = serviceProvider;

        public void Main(string[] args)
            // Allow the location of the ini file to be specified via a --config command line arg
            var tempBuilder = new ConfigurationBuilder().AddCommandLine(args);
            var tempConfig = tempBuilder.Build();
            var configFilePath = tempConfig[ConfigFileKey] ?? HostingIniFile;

            var appBasePath = _serviceProvider.GetRequiredService<IApplicationEnvironment>().ApplicationBasePath;
            var builder = new ConfigurationBuilder(appBasePath);
            builder.AddIniFile(configFilePath, optional: true);
            var config = builder.Build();

            var host = new WebHostBuilder(_serviceProvider, config).Build();
            using (host.Start())
                var appShutdownService = host.ApplicationServices.GetRequiredService<IApplicationShutdown>();
                Console.CancelKeyPress += (sender, eventArgs) =>
                    // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
                    eventArgs.Cancel = true;


  1. Sincere

    • 2016/7/1

    Katana's WebApp static class has been replaced by WebHostBuilder , that offers a much more flexible approach: 

  2. Reuben

    • 2017/11/9

    ASP.NET 5 / MVC 6 Console Hosted App. In MVC5, I had a console application that would use Microsoft.Owin.Hosting.WebApp.Start () to host a bunch of controllers that would be dynamically loaded from assemblies placed in an external folder and run some custom initialization on them via API call. This way I could pass parameters to the

  3. Callan

    • 2016/11/29

    In MVC5, I had a console application that would use Microsoft.Owin.Hosting.WebApp.Start() to host a bunch of controllers that would be dynamically loaded 

  4. Royal

    • 2017/9/13

    For more information, see .NET Generic Host in ASP.NET Core. Shutdown timeout host configuration setting when using Web Host. For more information, see ASP.NET Core Web Host. The hosted service is activated once at app startup and gracefully shut down at app shutdown.

  5. Colson

    • 2020/7/15

    NET console application with C# or Visual Basic using Visual Studio. Choose a .NET version .NET 5 .NET Core 3.1 .NET 6 Preview 

  6. Dangelo

    • 2018/5/5

    ASP.NET Core Runtime 5.0.0-preview.6. The ASP.NET Core Runtime enables you to run existing web/server applications. On Windows, we recommend installing the Hosting Bundle, which includes the .NET Runtime and IIS support. Full version 5.0.0-preview.6.20312.15 IIS runtime support (ASP.NET Core Module v2) 15.0.20164.0

  7. Roy

    • 2019/11/4

    Create a web app · Start Visual Studio and select Create a new project. · In the Create a new project dialog, select ASP.NET Core Web Application 

  8. Gomez

    • 2020/4/2

    With the release of .NET 5 and ASP.NET Core’s new hosting model, it has never been easier to host multiple web applications in a side-by-side configuration within one console host. In this short but concise post, let’s do just that. ASP.NET Core Console Host

  9. Bennett

    • 2020/5/20

    ASP.NET is an open-source, server-side web-application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to 

  10. Jake

    • 2019/1/30

    Host Settings. Host settings page is used to configure some system settings. General. Timezone is an important setting in this page. ASP.NET Zero can work in multiple zones. Each user can see dates and times in their own time zone. Timezone setting in this page allows you to set default time zone for the application including all tenants and users.

  11. Johnathan

    • 2021/7/1

    ASP.NET MVC is a web application framework developed by Microsoft that implements the model–view–controller (MVC) pattern. It is no longer in active 

  12. Allen

    • 2018/3/11

    NET 5 is the latest technology and learning it is made available to you through my course Complete ASP.Net Core and Entity Framework Development. Why Learn .NET MVC. Microsoft .NET is the platform that drives the business technology of many of the top corporations in the United States and many other countries.

  13. Kade

    • 2021/5/23

    (database, file, console) and change the logging configuration on-the-fly. ASP.NET core Logging. Write log messages to the ASP.NET Core Logging.

  14. Brantley

    • 2017/10/31

    The Model-View-Controller (MVC) architectural pattern separates an app into three main components: Model, View, and Controller. The MVC pattern helps you create apps that are more testable and easier to update than traditional monolithic apps. MVC-based apps contain: Models: Classes that represent the data of the app. The model classes use

  15. Leonel

    • 2016/5/2

    ASP.NET Core (previously ASP.NET 5) changes the way dependency injection NET Core 3.0, there's a “generic app hosting” mechanism in play that can be 

  16. Nehemiah

    • 2021/4/26

    Enabling always running feature for application hosted in Microsoft Azure is simpler a bit: NET Core app, you'd use No managed code, but if you do that, 

Comments are closed.

Recent Posts