コード例 #1
0
        public static void Main(string[] args)
        {
            //var builder = new ConfigurationBuilder()
            //    .SetBasePath(Directory.GetCurrentDirectory())
            //    .AddJsonFile("awesomeConfig.json")
            //    .AddJsonFile("awesomeConfig2.json");

            //Configuration = builder.Build();

            //foreach (var item in Configuration.AsEnumerable())
            //{
            //    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
            //}
            //Console.ReadKey();

            //load configuration from multiple sources and formats
            //var someSettings = new Dictionary<string, string>()
            //{
            //    { "poco:key1", "value 1" },
            //    { "poco:key2", "value 2" }
            //};
            //var builder = new ConfigurationBuilder()
            //    .SetBasePath(Directory.GetCurrentDirectory())
            //    .AddJsonFile("awesomeConfig.json")
            //    .AddJsonFile("awesomeConfig2.json")
            //    .AddXmlFile("awesomeConfig.xml")
            //    .AddIniFile("awesomeConfig.ini")
            //    .AddCommandLine(args)
            //    .AddEnvironmentVariables()
            //    .AddInMemoryCollection(someSettings)
            //    .AddUserSecrets("awesomeSecrets");

            //var config = builder.Build();

            //builder.AddAzureKeyVault(config["AzureKeyVault:url"], config["AzureKeyVault:clientId"], config["AzureKeyVault:secret"]);

            //Configuration = builder.Build();

            //var builder = new ConfigurationBuilder()
            //    .SetBasePath(Directory.GetCurrentDirectory())
            //    .AddLegacyXmlConfiguration("web.config");
            //add data protection
            var services = new ServiceCollection()
                           .AddDataProtection()
                           .Services.BuildServiceProvider();

            var protectedProvider = services.GetService <IDataProtectionProvider>();
            var protector         = protectedProvider.CreateProtector("AwesomePurpose")
                                    .ToTimeLimitedDataProtector();

            DateTimeOffset expiryDate;

            try
            {
                Console.Write($"Type something sensitive: ");
                var input          = Console.ReadLine();
                var protectedInput = protector.Protect(input, TimeSpan.FromSeconds(10));
                Console.WriteLine($"Protected: {protectedInput}");
                var unprotectedInput = protector.Unprotect(protectedInput, out expiryDate);
                Console.WriteLine($"Unprotected: {unprotectedInput}");
                Console.WriteLine();
            }
            catch (CryptographicException exception)
            {
                Console.WriteLine(exception.Message);
            }
            finally
            {
                Thread.Sleep(1000);
            }
            //
            //adding logger
            new WebHostBuilder()
            .UseKestrel()
            .UseStartup <Startup>()
            .ConfigureLogging(logging =>
            {
                logging.AddConsole(options => options.IncludeScopes = true);
                //logging.SetMinimumLevel(LogLevel.Information);
                logging.SetMinimumLevel(LogLevel.None)
                .AddFilter("Default", LogLevel.Error)
                .AddFilter <ConsoleLoggerProvider>("Program.Startup", LogLevel.Critical);

                logging.AddFilter(s => s == LogLevel.Warning)
                .AddFilter <ConsoleLoggerProvider>(s => s == LogLevel.Information);
            })
            .Build()
            .Run();
            //
            new WebHostBuilder()
            .UseKestrel()
            .UseStartup <Startup>()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((context, config) =>
            {
                config.AddJsonFile("logger.config.json");
            })
            .ConfigureLogging((context, logging) =>
            {
                var config = context.Configuration.GetSection("Logging");
                logging.AddConfiguration(config);
                logging.AddConsole();
            })
            .Build()
            .Run();
            //
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("awesomeConfig.json");

            var awesomeOptions = new AwesomeOptions();

            builder.Build().Bind(awesomeOptions);

            CreateWebHostBuilder(args).Build().Run();
        }
コード例 #2
0
 //IOptions<T> doesn't reflect config change
 //Instead we should use IOptionsSnapshot<T>
 public AwesomeController(IOptionsSnapshot <AwesomeOptions> options, IOptionsSnapshot <AwesomeOptions.BazOptions> bazOptions)
 {
     awesomeOptions  = options.Value;
     this.bazOptions = bazOptions.Value;
 }
コード例 #3
0
 public ConfigController(IOptionsSnapshot <AwesomeOptions> awesomeOptions, IOptions <AwesomeOptions.BazOptions> bazOptions)
 {
     this.Options    = awesomeOptions.Value;
     this.bazOptions = bazOptions.Value;
 }