[英]Configure IOptions<T> with ServiceCollection when T is Type
[英]ServiceCollection IOptions<> won't bind properly
我正在嘗試綁定一些可配置的設置。 這些值是通過 app-settings.local.json 提供的。 要綁定的 Model 稱為可配置設置。 我嘗試遵循一些教程並解決問題:
我試圖遵循此處給出的建議並將其應用到我自己的應用程序中。 經過 4 小時的嘗試,我無法讓它工作。 要么我缺乏實現這一點所需的基本知識,要么我忽略了一些東西。
我的 ConfigurableSettings class 的結構如下:
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; }
}
}
我的 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"
}
}
當我嘗試在 class 中使用它時,我在構造函數中調用它如下:
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);
//...
}
}
還要更改您的 model 設計,如下所示:
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; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.