Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A copy of Microsoft's WebHost.CreateDefaultBuilder but with IIS and FileWatcher dependencies removed for Linux environments. See
/// <summary>
/// Initializes a new instance of the <see cref="WebHostBuilder"/> class with pre-configured defaults.
/// </summary>
/// <remarks>
/// The following defaults are applied to the returned <see cref="WebHostBuilder"/>:
/// use Kestrel as the web server and configure it using the application's configuration providers,
/// set the <see cref="IHostingEnvironment.ContentRootPath"/> to the result of <see cref="Directory.GetCurrentDirectory()"/>,
/// load <see cref="IConfiguration"/> from 'appsettings.json',
/// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly,
/// load <see cref="IConfiguration"/> from environment variables,
/// load <see cref="IConfiguration"/> from supplied command line args,
/// configures the <see cref="ILoggerFactory"/> to log to the console and debug output,
/// and enables the ability for frameworks to bind their options to their default configuration sections.
/// </remarks>
/// <param name="args">The command line args.</param>
/// <returns>The initialized <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
var builder = new WebHostBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);
if (env.IsDevelopment())
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
config.AddUserSecrets(appAssembly, optional: true);
if (args != null)
.ConfigureLogging((hostingContext, logging) =>
.UseDefaultServiceProvider((context, options) =>
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
if (args != null)
builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
return builder;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment