...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
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
https://msdn.microsoft.com/en-us/library/microsoft.owin.hosting.webapp%28v=vs.113%29.aspx
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…