Skip to content
This repository has been 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

Closed
DamianEdwards opened this issue Apr 6, 2017 · 9 comments

Comments

@DamianEdwards
Copy link
Member

DamianEdwards commented Apr 6, 2017

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 and appsettings.[environment].json too
    • Add the developer exception page middleware via an IStartupFilter by default when IHostingEnvironment.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();
        }
   }
}

@davidfowl @glennc
aspnet/Hosting#991

@DamianEdwards DamianEdwards changed the title Add new WebHost static API to Microsoft.AspNetCore meta-package Add new WebHost static API to Microsoft.AspNetCore meta-package with opinionated defaults Apr 6, 2017
@lutzroeder
Copy link
Member

lutzroeder commented Apr 6, 2017

Adding .UseHandler() following the existing extension methods might be helpful as well skipping .Configure():

IWebHost webHost = new WebHostBuilder().UseKestrel().UseUrls(url).UseHandler(context => context.Response.WriteAsync("Hello, World!")).Build()

@DamianEdwards
Copy link
Member Author

@lutzroeder we don't have a concept of "handlers" in ASP.NET Core (although I course understand the intent) but the bigger issue is a method like that won't compose properly with the rest of the methods on IWebHostBuilder, i.e. you can't call it more than once, you can't call it along with .Configure(), etc. Once you go to the builder API, you have to configure the pipeline the normal way.

@DamianEdwards
Copy link
Member Author

@JunTaoLuo is this done now?

@DamianEdwards
Copy link
Member Author

@JunTaoLuo @davidfowl maybe we should add the debug logger by default too. Originally I think we planned to just merge it into the core logging package and always be on but it could just be enabled by this now. Thoughts?

@JunTaoLuo
Copy link
Contributor

JunTaoLuo commented Apr 22, 2017

I still need to add tests. I only merged the changes to unblock @CesarBS. I think we also found a bug that need to be fixed.

@DamianEdwards
Copy link
Member Author

@JunTaoLuo bugs? 🔥

@JunTaoLuo
Copy link
Contributor

Before closing the issue, I will also need to resolve the correct ApplicationName and pass it along to the builder when we wrap the RequestDelegate at https://github.com/aspnet/MetaPackages/blob/dev/src/Microsoft.AspNetCore/WebHost.cs#L38. Otherwise the correct ApplicationName gets overwritten to Microsoft.AspNetCore.

@DamianEdwards
Copy link
Member Author

@JunTaoLuo please ensure the DebugLogger is added by WebHost.CreateDefaultBuilder too. This needs to happen so that templates don't need to add it and the debug logging continues to work.

@JunTaoLuo
Copy link
Contributor

Functional tests added, and follow up issues have been filed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants