1. The memory configuration file assigns values to attributes
public static class Sample01
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public AppConfigDemo(IConfiguration config)
{
Name = config["Name"];
StartDate = config["StartDate"];
EndDate = config["EndDate"]; }}public static void Run()
{
var source = new Dictionary<string.string> {["Name"] = "AppConfig"["StartDate"] = "1 May 2020"["EndDate"] = "5 May 2020"
};
// Add the memory configuration file
var config = new ConfigurationBuilder()
// .Add(new MemoryConfigurationSource{InitialData = source})
.AddInMemoryCollection(source)
.Build();
var options = new AppConfigDemo(config);
Console.WriteLine($"Name:{options.Name}");
Console.WriteLine($"StartDate:{options.StartDate}");
Console.WriteLine($"EndDate:{options.EndDate}"); }}Copy the code
2. Bind objects to memory configuration files
public static class Sample02
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }}public static void Run()
{
var source = new Dictionary<string.string> {["Name"] = "AppConfig"["StartDate"] = "1 May 2020"["EndDate"] = "5 May 2020"
};
var options = new ConfigurationBuilder()
.AddInMemoryCollection(source)
.Build()
.Get<AppConfigDemo>();
Console.WriteLine($"Name:{options.Name}");
Console.WriteLine($"StartDate:{options.StartDate}");
Console.WriteLine($"EndDate:{options.EndDate}"); }}Copy the code
3. Configure the binding object in a JSON file
public static class Sample03
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public Behavior Behavior { get; set; }}public class Behavior
{
public bool IsRead { get; set; }
public bool IsWrite { get; set; }}public static void Run()
{
var options = new ConfigurationBuilder()
// Add the Json configuration file
//.Add(new JsonConfigurationSource{Path = "appsettings.json"})
.AddJsonFile("appsettings.json")
.Build()
.Get<AppConfigDemo>();
Console.WriteLine($"Name:{options.Name}");
Console.WriteLine($"StartDate:{options.StartDate}");
Console.WriteLine($"EndDate:{options.EndDate}");
Console.WriteLine($"Behavior.IsRead:{options.Behavior.IsRead}");
Console.WriteLine($"Behavior.IsWrite:{options.Behavior.IsWrite}"); }}Copy the code
4. Monitor configuration files
public static class Sample04
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public Behavior Behavior { get; set; }}public class Behavior
{
public bool IsRead { get; set; }
public bool IsWrite { get; set; }}public static void Run()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json".true.true)
.Build();
Read(config.Get<AppConfigDemo>());
ChangeToken.OnChange(() => config.GetReloadToken(), () =>
{
Read(config.Get<AppConfigDemo>());
});
Console.Read();
}
public static void Read(AppConfigDemo options)
{
Console.Clear();
Console.WriteLine($"Name:{options.Name}");
Console.WriteLine($"StartDate:{options.StartDate}");
Console.WriteLine($"EndDate:{options.EndDate}");
Console.WriteLine($"Behavior.IsRead:{options.Behavior.IsRead}");
Console.WriteLine($"Behavior.IsWrite:{options.Behavior.IsWrite}"); }}Copy the code
5. Environment variable configuration file
public static class Sample05
{
public class AppConfigDemo
{
public string Name { get; set; }
public string Ver { get; set; }}public static void Run()
{
Environment.SetEnvironmentVariable("APP_NAME"."AppDemo");
Environment.SetEnvironmentVariable("APP_VER"."Alpha");
var config = new ConfigurationBuilder()
.AddEnvironmentVariables("APP_")
.Build()
.Get<AppConfigDemo>();
Console.WriteLine($"Name:{config.Name}");
Console.WriteLine($"Ver:{config.Ver}"); }}Copy the code
6. Command line variable configuration file
public static class Sample06
{
public class AppConfigDemo
{
public string Name { get; set; }
public string Version { get; set; }}public static void Run(string[] args)
{
var mapping = new Dictionary<string.string> () {// Parameter mapping
["-n"] = "Name"["-ver"] = "Version"
};
var config = new ConfigurationBuilder()
.AddCommandLine(args, mapping)
.Build()
.Get<AppConfigDemo>();
Console.WriteLine($"Name:{config.Name}");
Console.WriteLine($"Ver:{config.Version}"); }}Copy the code
7. The XML file configures the binding object
public static class Sample07
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public Behavior Behavior { get; set; }}public class Behavior
{
public bool IsRead { get; set; }
public bool IsWrite { get; set; }}public static void Run()
{
var options = new ConfigurationBuilder()
.AddXmlFile("App.config")
.Build()
.GetSection(nameof(AppConfigDemo))// Get the configuration name
.Get<AppConfigDemo>();
Console.WriteLine($"Name:{options.Name}");
Console.WriteLine($"StartDate:{options.StartDate}");
Console.WriteLine($"EndDate:{options.EndDate}");
Console.WriteLine($"Behavior.IsRead:{options.Behavior.IsRead}");
Console.WriteLine($"Behavior.IsWrite:{options.Behavior.IsWrite}"); }}Copy the code
8.Options Use the configuration file
public static class Sample01
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public Behavior Behavior { get; set; }}public class Behavior
{
public bool IsRead { get; set; }
public bool IsWrite { get; set; }}public static void Run()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var serviceProvider = new ServiceCollection()
.AddOptions()// Add options service
.Configure<AppConfigDemo>(configuration)// Bind options
.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<AppConfigDemo>>();
var appConfig = options.Value;
Console.WriteLine($"Name:{appConfig.Name}");
Console.WriteLine($"StartDate:{appConfig.StartDate}");
Console.WriteLine($"EndDate:{appConfig.EndDate}");
Console.WriteLine($"Behavior.IsRead:{appConfig.Behavior.IsRead}");
Console.WriteLine($"Behavior.IsWrite:{appConfig.Behavior.IsWrite}"); }}Copy the code
9.Options Anonymously use multiple configuration files
public static class Sample02
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }}public static void Run()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("muti.json")
.Build();
var serviceProvider = new ServiceCollection()
.AddOptions()
.Configure<AppConfigDemo>("DefaultApp", configuration.GetSection("DefaultApp"))
.Configure<AppConfigDemo>("CustomApp", configuration.GetSection("CustomApp"))
.BuildServiceProvider();
// An object with naming options
var options = serviceProvider.GetRequiredService<IOptionsSnapshot<AppConfigDemo>>();
var defaultApp = options.Get("DefaultApp");
var customApp = options.Get("CustomApp");
Console.WriteLine($"Name:{defaultApp.Name}");
Console.WriteLine($"StartDate:{defaultApp.StartDate}");
Console.WriteLine($"EndDate:{defaultApp.EndDate}");
Console.WriteLine($"Name:{customApp.Name}");
Console.WriteLine($"StartDate:{customApp.StartDate}");
Console.WriteLine($"EndDate:{customApp.EndDate}"); }}Copy the code
10.Options Monitoring function configuration file
public static class Sample03
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public Behavior Behavior { get; set; }}public class Behavior
{
public bool IsRead { get; set; }
public bool IsWrite { get; set; }}public static void Run()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json".false.true)
.Build();
var serviceProvider = new ServiceCollection()
.AddOptions()
.Configure<AppConfigDemo>(configuration)
.BuildServiceProvider();
// Object with monitoring options
var options = serviceProvider.GetRequiredService<IOptionsMonitor<AppConfigDemo>>();
options.OnChange(appConfig =>
{
Console.Clear();
Console.WriteLine($"Name:{appConfig.Name}");
Console.WriteLine($"StartDate:{appConfig.StartDate}");
Console.WriteLine($"EndDate:{appConfig.EndDate}");
Console.WriteLine($"Behavior.IsRead:{appConfig.Behavior.IsRead}");
Console.WriteLine($"Behavior.IsWrite:{appConfig.Behavior.IsWrite}"); }); Console.Read(); }}Copy the code
11. Initialize the value of the Options configuration file
public static class Sample05
{
public class AppConfigDemo
{
public string Name { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }}public static void Run()
{
var serviceProvider = new ServiceCollection()
.AddOptions()
.Configure<AppConfigDemo>(config =>
{
config.Name = "DefaultApp";
config.StartDate = "2020/5/6";
config.EndDate = "2020/5/7";
})
.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<AppConfigDemo>>();
var appConfig = options.Value;
Console.WriteLine($"Name:{appConfig.Name}");
Console.WriteLine($"StartDate:{appConfig.StartDate}");
Console.WriteLine($"EndDate:{appConfig.EndDate}"); }}Copy the code
12. Verify the configuration file with the Options parameter
public static class Sample06
{
public class AppConfigDemo
{
public string Name { get; set; }
public string Version { get; set; }}public static void Run(string[] args)
{
var mapping = new Dictionary<string.string>()
{
["-n"] = "Name"["-ver"] = "Version"
};
var config = new ConfigurationBuilder()
.AddCommandLine(args, mapping)
.Build();
var services = new ServiceCollection();
services.AddOptions<AppConfigDemo>()
.Configure(options =>
{
options.Name = config["Name"]????"";
options.Version = config["Version"]????"";
})
.Validate(demo => demo.Version.StartsWith("Alpha"), "Version parameter invalid")
.Validate(demo => demo.Name.EndsWith("App"), "Invalid Name parameter");
try
{
varoptions = services.BuildServiceProvider() .GetRequiredService<IOptions<AppConfigDemo>>().Value; Console.WriteLine(options.Name); Console.WriteLine(options.Version); } catch (OptionsValidationException ex) { Console.WriteLine(ex.Message); }}}Copy the code