This repository was archived by the owner on Nov 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
This repository was archived by the owner on Nov 21, 2018. It is now read-only.
Add new WebHost static API to Microsoft.AspNetCore meta-package with opinionated defaults #24
Copy link
Copy link
Closed
Description
Add new static API WebHost
to support creating opinionated instances of IWebHost
and IWebHostBuilder
which default the following over and above the usual defaults:
- Server to Kestrel
- Content root to working directory
- Other ideas for defaults (in ascending order of craziness):
- Log to console
- Add cmd line and environment variable configuration providers, maybe even JSON for
appsettings.json
andappsettings.[environment].json
too - Add the developer exception page middleware via an
IStartupFilter
by default whenIHostingEnvironment.IsDevelopment()
There would be methods that allow building and starting an IWebHost
with one call, and another that allows creating an IWebHostBuilder
for more advanced scenarios, e.g. setting up configuration, logging, etc.
namespace Microsoft.AspNetCore
{
public static class WebHost
{
public static IWebHost Start(RequestDelegate app) { }
public static IWebHost Start(string url, RequestDelegate app) { }
public static IWebHost StartWith(Action<IApplicationBuilder> app) { }
public static IWebHost StartWith(string url, Action<IApplicationBuilder> app) { }
public static IWebHostBuilder CreateBuilder() { }
}
}
They would be used like so:
namespace MyApp
{
public static class Program
{
public static void Main(string[] args)
{
// Hello, World!
var host1 = WebHost.Start(context => context.Response.WriteAsync("Hello, World!"));
host1.WaitForShutdown();
// Hello, World! + middleware
var host2 = WebHost.StartWith(app =>
{
var env = app.ApplicationServices.GetService<IHostingEnvironment>();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage()
}
app.Run(context => context.Response.WriteAsync("Hello, World!"))
);
host2.WaitForShutdown();
// Getting a builder to do something more complex, the templates would do this
var host3 = WebHost.CreateBuilder()
.UseLogging(logging => logging
.AddConsole()
.AddDebug()
)
.UseStartup<Startup>()
.Build();
host3.Run();
}
}
}
Some slightly more audacious ideas
Add overloads that allow easy creation of an app that uses a single routed middleware:
public static IWebHost Start(Action<IRouteBuilder> routes) { }
public static IWebHost Start(string url, Action<IRouteBuilder> routes) { }
Used like so:
namespace MyApp
{
public static class Program
{
public static void Main(string[] args)
{
// Routed middleware
var host = WebHost.Start(router => router
.MapGet("/hello/{name}", (req, res, data) => res.WriteAsync($"Hello, {data["name"]}))
.MapGet("/goodbye/{name}", (req, res, data) => res.WriteAsync($"Goodbye, {data["name"]}))
.MapGet("/{greeting}/{name}", (req, res, data) => res.WriteAsync($"{greeting}, {data["name"]}))
.MapGet("/", (req, res, data) => res.WriteAsync($"Hello, World!"))
);
host.WaitForShutdown();
}
}
}