diff --git a/src/Microsoft.AspNetCore/WebHost.cs b/src/Microsoft.AspNetCore/WebHost.cs index 9b576a70fa..99b414104d 100644 --- a/src/Microsoft.AspNetCore/WebHost.cs +++ b/src/Microsoft.AspNetCore/WebHost.cs @@ -187,5 +187,40 @@ namespace Microsoft.AspNetCore return builder; } + + /// + /// Initializes a new instance of the class with pre-configured defaults using typed Startup + /// + /// Specify the startup type to be used by the web host. + /// The command line args. + /// + /// The following defaults are applied to the returned : + /// use Kestrel as the web server, + /// set the to the result of , + /// load from 'appsettings.json' and 'appsettings.[].json', + /// load from User Secrets when is 'Development' using the entry assembly, + /// load from environment variables, + /// configures the to log to the console and debug output, + /// enables IIS integration, + /// enables the ability for frameworks to bind their options to their default configuration sections, + /// adds the developer exception page when is 'Development' + /// and sets the startup class as the typed defined in T. + /// + /// The initialized . + public static IWebHostBuilder CreateDefaultBuilder(string[] args) where T : class + { + return WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } + + /// + /// Builds an Microsoft.AspNetCore.Hosting.IWebHost which hosts a web application and + /// runs a web application and block the calling thread until host shutdown. + /// + /// The + public static void BuildAndRun(this IWebHostBuilder builder) + { + builder.Build().Run(); + } } } diff --git a/test/TestSites/CreateDefaultBuilderApp/Program.cs b/test/TestSites/CreateDefaultBuilderApp/Program.cs index ab302e8f96..1585086319 100644 --- a/test/TestSites/CreateDefaultBuilderApp/Program.cs +++ b/test/TestSites/CreateDefaultBuilderApp/Program.cs @@ -36,6 +36,11 @@ namespace CreateDefaultBuilderApp }); }) .Build().Run(); + + Console.ReadKey(); + WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" }) + .BuildAndRun(); + } private static string GetResponseMessage(WebHostBuilderContext context, IServiceCollection services) diff --git a/test/TestSites/CreateDefaultBuilderApp/Startup.cs b/test/TestSites/CreateDefaultBuilderApp/Startup.cs new file mode 100644 index 0000000000..c44a15ee8c --- /dev/null +++ b/test/TestSites/CreateDefaultBuilderApp/Startup.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; + +namespace CreateDefaultBuilderApp +{ + public class Startup + { + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + app.Run(async (context) => + { + await context.Response.WriteAsync("Hello World!"); + }); + } + } +}