在.net Core 1中我们可以这样做:
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.Build();
Run Code Online (Sandbox Code Playgroud)
这使我们可以在我们的控制台应用程序中使用Configuration对象.
.net core 2.0的所有示例似乎都是针对Asp.Net核心配置创建的新方式而定制的.
为控制台应用程序创建配置的方法是什么?
更新:此问题与Asp.net核心无关.编辑时请不要添加asp.net核心标记.
我正在 .Net Core 中学习 DI,但我不知道使用 .Net Core 的好处 IOptions。
IOptions如果我们可以没有它,为什么我们需要它?
IOptionsinterface IService
{
void Print(string str);
}
class Service : IService
{
readonly ServiceOption options;
public Service(IOptions<ServiceOption> options) => this.options = options.Value;
void Print(string str) => Console.WriteLine($"{str} with color : {options.Color}");
}
class ServiceOption
{
public bool Color { get; set; }
}
class Program
{
static void Main()
{
using (ServiceProvider sp = RegisterServices())
{
//
}
}
static ServiceProvider RegisterServices()
{
IServiceCollection isc = …Run Code Online (Sandbox Code Playgroud)