62

I have a full-working ASP.NET MVC application (.NET Core, ASP.NET Core) which runs fine in Visual Studio (which uses IISExpress).

I would now like to have a console application which takes the ASP.NET Core application and hosts it (self hosting).

Pang
  • 9,564
  • 146
  • 81
  • 122
selva
  • 887
  • 2
  • 7
  • 11
  • 3
    See the instructions at the end of this article: http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6 The same steps apply to mvc and web api as they are the same type of project in asp.net 5 – rdans May 18 '15 at 08:41
  • when i use web listener to host application, i got the following exception. System.MissingMethodException: Method not found: 'Int32 System.Runtime.InteropSe rvices.Marshal.SizeOf(!!0)'. at Microsoft.Net.Http.Server.WebListener.SetRequestQueueLimit() at Microsoft.Net.Http.Server.WebListener.Start() at Microsoft.AspNet.Server.WebListener.MessagePump.Start(Func`2 app) at Microsoft.AspNet.Server.WebListener.ServerFactory.Start(IServerInformation server, Func`2 app) at Microsoft.AspNet.Hosting.HostingEngine.Start(HostingContext context) – selva May 18 '15 at 12:10
  • Welcome to Stack Overflow, @selva! This is an inherent feature of the [tag:asp.net-5] build, as @rdans said. If you're having a specific issue with getting the linked tutorial working, please ask that question, instead. – Matt DeKrey May 18 '15 at 12:35
  • I found this article which explains 6 different ways to run an ASP.Net Core application: http://www.secretgeek.net/dotnet_run – DarkAngel Oct 24 '17 at 09:16

3 Answers3

46

Is it possible to self-host an ASP.NET Core Application without IIS?

Yes. In fact, all ASP.NET Core applications are self-hosted. Even in production, IIS/Nginx/Apache are a reverse proxy for the self-hosted application.

In a reasonably standard Program.cs class, you can see the self-hosting. The IISIntegration is optional - it's only necessary if you want to integrate with IIS.

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddCommandLine(args)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 1
    Shouldn't this answer be updated to reflect .NET Core 1.0 RTM? So instead of `dnu`, use `dotnet` e.g. – QuantumHive Jul 27 '16 at 19:24
  • 1
    If you follow the Yeoman approach, make sure that you have the proper .NET Core version. When I first installed the [Core for VS 2015](https://www.microsoft.com/net/core), I got the v1.0.1 installed, and when I run the `dotnet run` command, I realized that the MVC 6 Basic WebApp requires the .NET Core 1.1.0. After getting the .NET Core 1.1 runtime installer from [here](https://www.microsoft.com/net/download/core#/current/runtime), I could finally run the sample project. – Andras Dec 14 '16 at 10:46
  • aspnet/cli-samples.git is not available, is this moved? – Neel May 25 '17 at 07:18
14

Yes,ASP.NET Core supports the Open Web Interface for .NET (OWIN),your have two options to host your Asp.net core web application:

  1. IIS

  2. Self-Host

But,self-hosting web application can't restart automatically on system boot and restart or in the event of a failure.

brichins
  • 3,825
  • 2
  • 39
  • 60
chen
  • 181
  • 2
  • 7
4

YES

ASP.NET 5 is completely decoupled from the web server environment that hosts the application. ASP.NET 5 supports hosting in IIS and IIS Express, and self-hosting scenarios using the Kestrel and WebListener HTTP servers. Additionally, developers and third party software vendors can create custom servers to host their ASP.NET 5 apps.

more info: ASP.NET documentation - Servers

Søren
  • 6,517
  • 6
  • 43
  • 47