Skip to content

Instantly share code, notes, and snippets.

@guardrex
Last active September 2, 2017 15:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save guardrex/f40f1b1414e175a6514f to your computer and use it in GitHub Desktop.
Save guardrex/f40f1b1414e175a6514f to your computer and use it in GitHub Desktop.
My "NUTJOB" WebHostBuilder Pattern
/*
The purpose of this pattern is to consolidate all application
configuration into just ONE file and configure the app based
on the server/dev environment where it finds itself. It removes
the need for environment variables and/or JSON config files on
individual machines.
*/
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Logging;
namespace MyApp
{
public class AppOptions
{
public string MachineName { get; set; }
public string DevelopmentIp { get; set; }
public string Application { get; set; }
public string MyOption1 { get; set; }
public string MyOption2 { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
// Set the local environment based on the server's name & establish configuration
string localEnvironment;
string machineName = Environment.MachineName;
var configurationBuilder = new ConfigurationBuilder().AddEnvironmentVariables();
IConfiguration Configuration = configurationBuilder.Build();
if (machineName.StartsWith("<a_production_vm_prefix>", StringComparison.OrdinalIgnoreCase) ||
machineName.StartsWith("<another_production_vm_prefix>", StringComparison.OrdinalIgnoreCase))
{
localEnvironment = "Production";
Configuration["ConfigKey1"] = "Production: Config Value1";
Configuration["ConfigKey2"] = "Production: Config Value2";
}
else if (machineName.StartsWith("<a_staging_vm_prefix>", StringComparison.OrdinalIgnoreCase))
{
localEnvironment = "Staging";
Configuration["ConfigKey1"] = "Staging: Config Value1";
Configuration["ConfigKey2"] = "Staging: Config Value2";
}
else
{
localEnvironment = "Development";
Configuration["ConfigKey1"] = "Development: Config Value1";
Configuration["ConfigKey2"] = "Development: Config Value2";
}
// Build the Application
// Add .UseConfiguration(config) to WebHostBuilder if command line arguments
// desired for configuration.
var host = new WebHostBuilder()
.ConfigureLogging(options =>
{
if (localEnvironment == "Production")
{
options.AddConsole(LogLevel.Warning);
}
else
{
options.AddConsole(LogLevel.Debug);
}
})
.UseKestrel(options => { options.AddServerHeader = false; })
.UseContentRoot(Directory.GetCurrentDirectory())
.UseEnvironment(localEnvironment)
.UseIISIntegration()
.ConfigureServices(services =>
{
services.AddDataProtection().SetApplicationName("www_myapp_com");
services.AddAntiforgery();
services.AddSingleton(_ => Configuration);
services.Configure<AppOptions>(appOptions =>
{
appOptions.Application = "www.myapp.com";
appOptions.MachineName = machineName;
appOptions.DevelopmentIp = "1.1.1.1";
if (localEnvironment == "Production")
{
appOptions.MyOption1 = "Production: Options Value1";
appOptions.MyOption2 = "Production: Options Value2";
}
else if (localEnvironment == "Staging")
{
appOptions.MyOption1 = "Staging: Options Value1";
appOptions.MyOption2 = "Staging: Options Value2";
}
else
{
appOptions.MyOption1 = "Development: Options Value1";
appOptions.MyOption2 = "Development: Options Value2";
}
});
services.AddMvc(options =>
{
if (localEnvironment == "Production")
{
options.CacheProfiles.Add("PublicCache", new CacheProfile()
{
Duration = 2592000,
Location = ResponseCacheLocation.Any
});
}
else
{
options.CacheProfiles.Add("PublicCache", new CacheProfile()
{
Duration = 0,
Location = ResponseCacheLocation.None
});
}
});
})
.Configure((app) =>
{
if (localEnvironment == "Production")
{
app.UseExceptionHandler("/Error");
}
else
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
})
.Build();
host.Run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment