nuget 多安装 Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Options
https://ballardsoftware.com/adding-appsettings-json-configuration-to-a-net-core-console-application/ 怎么绑定 json config 文件 ,但不能实时更新
http://www.10tiao.com/html/391/201901/2654073587/4.html
https://stackoverflow.com/questions/38515965/cannot-set-configuration-from-json-appsettings-file-in-net-core-project
https://offering.solutions/blog/articles/2017/02/17/automatically-reload-typed-configuration-in-asp-net-core/ asp 的做法 , 但不是 c# console-application/
https://stackoverflow.com/questions/51969527/ioptionssnapshot-in-net-core-console-application-not-working 已经很接近的问题
当config值改变时, 能通知应用程序
https://stackoverflow.com/questions/53604397/how-can-i-build-an-ioptionsmonitort-for-testing
例子 : https://github.com/aspnet/Logging/issues/874
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
using System; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Binder; using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System.Threading; using Newtonsoft.Json; namespace ConsoleApp1 { class Program { //static void ConfigureServices(IServiceCollection serviceCollection) //{ // // build configuration // var configuration = new ConfigurationBuilder() // .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // .Build(); // serviceCollection.AddSingleton() // serviceCollection.Configure<AppSettings>(configuration.GetSection("Configuration")); // // add services // serviceCollection.AddTransient<ITestService, TestService>(); // // add app // serviceCollection.AddTransient<App>(); //} //private static IServiceProvider provider; //private static EventReceiver receiver; static void Main(string[] args) { var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); var services = new ServiceCollection(); //services.AddLogging(b => //{ // b.AddConfiguration(config); // b.AddConsole(); //}); services.AddOptions(); services.Configure<MySettings>(config.GetSection("MySettings")); using (var sp = services.BuildServiceProvider()) { var options = sp.GetRequiredService<IOptionsMonitor<MySettings>>(); options.OnChange((o, name) => Console.WriteLine($"Name = {name}, Hash = {o.GetHashCode()}, Content = {JsonConvert.SerializeObject(o)}")); } Console.ReadLine(); /* string sURL; var build = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); // IConfiguration config = new ConfigurationBuilder() //.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) //.Build(); IConfigurationRoot configuration = build.Build(); IServiceProvider provider = new ServiceCollection() .Configure<MySettings>( configuration.GetSection("MySettings")) .AddTransient<CheckOption>() .BuildServiceProvider(); //var services = new ServiceCollection(); //services.AddOptions(); //services.Configure<MySettings>(configuration.GetSection("MySettings")); //var sp = services.BuildServiceProvider(); //sURL = configuration["LocalServer"]; while (true) { Thread.Sleep(1000); //configuration.Reload(); //sURL = configuration["LocalServer"]; CheckOption checkOption = provider.GetRequiredService<CheckOption>(); //checkOption.Run(); //IOptionsSnapshot<MySettings> settings = sp.GetRequiredService<IOptionsSnapshot<MySettings>>(); //var options = sp.GetService<IOptionsSnapshot<MySettings>>(); //Console.WriteLine(options.Value.CurrentServer); //Console.WriteLine(settings.Value.CurrentServer); Console.WriteLine(checkOption.Run()); //Console.WriteLine(sERRNAP); } //// create service collection //var serviceCollection = new ServiceCollection(); //ConfigureServices(serviceCollection); //// create service provider //var serviceProvider = serviceCollection.BuildServiceProvider(); //// entry to run app //serviceProvider.GetService<App>().Run(); *//// } private static void ConfigureServices(IServiceCollection services) { } } public class CheckOption { public MySettings _AppSettings; public CheckOption(IOptionsSnapshot<MySettings> appSettings) { _AppSettings = appSettings.Value; } public string Run() { string option1 = _AppSettings.CurrentServer; //string option2 = _AppSettings.Option2; return option1; } } //public class CheckOption //{ // public AppSettings _AppSettings; // public CheckOption(IOptionsSnapshot<AppSettings> appSettings) // { // _AppSettings = appSettings.Value; // } // public void Run() // { // string option1 = _AppSettings.Option1; // string option2 = _AppSettings.Option2; // } //} public class MySettings { public string CurrentServer { get; set; } } } |
配套配置文件 :
1 2 3 4 5 6 |
{ "MySettings": { "CurrentServer": "XBACTaaabbbccXXX" }, "LocalServer": "XBACTwwwaaa" } |