-
-
Save moshensky/6d706472d35b36c27e4c8cdb9fe20614 to your computer and use it in GitHub Desktop.
My "NUTJOB" WebHostBuilder Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
// Note: Might need to add .UseConfiguration(config) to WebHostBuilder if command line | |
// arguments are desired for configuration. | |
var host = new WebHostBuilder() | |
.ConfigureLogging(options => | |
{ | |
if (localEnvironment == "Production") | |
{ | |
options.AddConsole(LogLevel.Warning); | |
} | |
else | |
{ | |
options.AddConsole(LogLevel.Debug); | |
} | |
}) | |
.UseKestrel() | |
.UseEnvironment(localEnvironment) | |
.UseIISIntegration() | |
.ConfigureServices(services => | |
{ | |
services.AddDataProtection().SetApplicationName("www_myapp_com"); | |
services.AddAntiforgery(); | |
services.AddSingleton(_ => Configuration); | |
services.AddOptions(); | |
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.Use((context, next) => | |
{ | |
context.Response.Headers.Remove("Server"); | |
return next(); | |
}); | |
app.UseMvcWithDefaultRoute(); | |
}) | |
.Build(); | |
host.Run(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment