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
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
BuildWebHost(args).Run(); | |
} | |
public static IWebHost BuildWebHost(string[] args) | |
{ | |
var webhost = WebHost.CreateDefaultBuilder(args); |
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
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddOptions(); | |
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings")); | |
services.AddSingleton(Configuration); | |
// In a real implementation, you would have a factory here to get the password and salt securely | |
// from somewhere such as Azure Key Vault, Environmental variable / another json setting (obfuscated in some way) | |
services.AddSingleton( x => new CryptoFactory().Create<AesManaged>("Password", "Salt")); |
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
public class HomeController : Controller | |
{ | |
private readonly IMyAppSettings _settings; | |
public HomeController(IMyAppSettings settings) | |
{ | |
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); | |
} | |
public IActionResult Index() | |
{ |
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
public class Startup | |
{ | |
// Other methods omitted for brevity in example | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddOptions(); | |
// use the reader class to map the configuration | |
services.Configure<MyAppSettingsReader>(Configuration.GetSection("MyAppSettings")); |
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
public class MyAppSettingsReader : IMyAppSettings | |
{ | |
public string ApplicationName { get; set; } | |
public int CountOfItems { get; set; } | |
} | |
public class MyAppSettingsBridge : IMyAppSettings | |
{ | |
private readonly IOptionsSnapshot<MyAppSettingsReader> _optionsConfig; |
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
public interface IMyAppSettings | |
{ | |
string ApplicationName { get; } | |
int CountOfItems { get; } | |
} |
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
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddOptions(); | |
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings")); | |
} |
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
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; private set; } | |
// This method gets called by the runtime. Use this method to add services to the container. |
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
public class HomeController : Controller | |
{ | |
private readonly MyAppSettings _settings; | |
public HomeController(IOptionsSnapshot<MyAppSettings> settings) | |
{ | |
_settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings)); | |
} | |
public IActionResult Index() | |
{ |
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
public class HomeController : Controller | |
{ | |
private readonly MyAppSettings _settings; | |
public HomeController(MyAppSettings settings) | |
{ | |
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); | |
} | |
public IActionResult Index() | |
{ |