我正在尝试绑定一些可配置的设置。这些值是通过app-settings.local.json提供的。要绑定到的模型称为可配置设置。我试图遵循一些教程并解决问题:
- https://andrewlock.net/how-to-use-the-ioptions-pattern-for-configuration-in-asp-net-core-rc2/
- https://github.com/aspnet/Configuration/issues/411
- 无法从.NET Core项目中的JSON appsettings文件设置配置
- ServiceCollection为IOptions返回null,即使GetSection正在工作
我试图遵循这里给出的建议,并将其应用于我自己的应用程序中。经过4个小时的努力,我还是没能让它发挥作用。要么我缺乏实现这一点所需的基本知识,要么我忽略了一些东西。
我的ConfigurationSettings类的结构如下:
public class ConfigurableSettings
{
public AppSettings _AppSettings;
public DgeSettings _DgeSettings;
public class AppSettings
{
[JsonProperty("SoftDelete")]
public bool SoftDelete { get; set; }
}
public class DgeSettings
{
[JsonProperty("zipFileUrl")]
public string zipFileUrl { get; set; }
[JsonProperty("sourceFileUrl")]
public string sourceFileUrl { get; set; }
}
}
My ConfigureServices的结构如下:
public static void ConfigureServices(IServiceCollection serviceCollection, string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddOptions();
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("app-settings.local.json", true)
.AddJsonFile("app-settings.json", false)
.Build();
serviceCollection.Configure<ConfigurableSettings>(options => configuration.GetSection("AppSettings").Bind(options));
serviceCollection.Configure<ConfigurableSettings>(options => configuration.GetSection("DgeSettings").Bind(options));
var services = serviceCollection.BuildServiceProvider();
var options = services.GetService<IOptions<ConfigurableSettings>>();
serviceCollection.AddLogging(loggingBuilder =>
{
loggingBuilder.AddConfiguration(configuration.GetSection("Logging"));
loggingBuilder.AddConsole();
loggingBuilder.AddDebug();
});
serviceCollection.AddServices(configuration);
serviceCollection.AddNopCommerceServices(configuration);
serviceCollection.AddTransient<Comparator>();
serviceCollection.AddTransient<UpdateManager>();
serviceCollection.AddTransient<DgeRequestAuthenticator>();
serviceCollection.AddTransient<ISourceConnector, DgeConnector>();
serviceCollection.AddTransient<IDestinationConnector, NopCommerceConnector>();
}
我的app-settings.local.json配置如下:
{
"AppSettings": {
"SoftDelete": true
},
"DgeSettings": {
"zipFileUrl" : "www.example-url.com",
"sourceFileUrl" : "www.example-url.com"
}
}
当我试图在类中使用它时,我在构造函数中调用它,如下所示:
private readonly ConfigurableSettings _settings;
public AlphaBetaService(IOptions<ConfigurableSettings> settings)
{
_settings = settings.Value;
}
有人能帮我找出我做错了什么吗?
不确定如何在项目中使用ConfigureServices
方法,实际上Startup.cs中的ConfigureServices
必须是无参数的,或者只接受一个类型为IServiceCollection
的参数。
更改您的ConfigureServices,如下所示:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("app-settings.local.json", true)
.Build();
services.Configure<ConfigurableSettings>(configuration);
//...
}
}
还可以更改您的模型设计,如下所示:
public class ConfigurableSettings
{
public AppSettings AppSettings{ get; set; }
public DgeSettings DgeSettings { get; set; }
}
public class AppSettings
{
[JsonProperty("SoftDelete")]
public bool SoftDelete { get; set; }
}
public class DgeSettings
{
[JsonProperty("zipFileUrl")]
public string zipFileUrl { get; set; }
[JsonProperty("sourceFileUrl")]
public string sourceFileUrl { get; set; }
}