【发布时间】:2020-07-20 06:33:50
【问题描述】:
在读取 json 设置文件的部分时,是否可以配置 .Net Core 3.1 控制台应用程序以使用 Newtonsoft.Json 库来反序列化 IOptions(来自 Microsoft.Extensions.Options)?
public MyService(IOptions<MyAppDataSettings> MyAppDataOptions)
{
var myAppDataOptions = MyAppDataOptions?.Value ?? throw new ArgumentNullException(nameof(MyAppDataSettings));
}
与直接用System.Text.Json反序列化时的结果不同:
var appJsonData = System.Text.Json.JsonSerializer.Deserialize<MyAppDataSettings>(File.ReadAllText(appJsonPath));
设置包含“有序”字典。直接调用反序列化器时,键的顺序是正确的,和json设置文件中一样。但是 IOptions 返回的值包含一个字典,其中的键按字母顺序排序。 然后我尝试强制使用 NewtonsoftJson:
private static void ConfigureServices(IConfiguration configuration, IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.Configure<MyAppDataSettings>(configuration.GetSection(nameof(MyAppDataSettings)));
services.AddSingleton<IMyService, MyService>();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
new HostBuilder()
.ConfigureServices((context, services) =>
{
ConfigureServices(context.Configuration, services);
})
.ConfigureAppConfiguration((context, configurationBuilder) =>
{
var appExecPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
var appSettingsPath = Path.GetFullPath(Path.Combine(appExecPath, @"Settings"));
configurationBuilder
.SetBasePath(appSettingsPath)
.AddJsonFile("MySettings.json", false);
});
使用 Newtonsoft.Json 反序列化器时的结果与使用 System.Text.Json 时的结果相同。
var appJsonData = JsonConvert.DeserializeObject<MyAppDataSettings>(File.ReadAllText(appJsonPath));
所以问题是:IOptions 是如何反序列化的?我想“魔法”发生在 Microsoft.Extensions.Configuration.Json 库中。
【问题讨论】:
标签: json .net-core dependency-injection console-application settings