相关疑难解决方法(0)

控制台应用程序的配置.net Core 2.0

在.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核心标记.

c# .net-core .net-core-2.0

32
推荐指数
2
解决办法
2万
查看次数

我们什么时候需要 IOptions?

我正在 .Net Core 中学习 DI,但我不知道使用 .Net Core 的好处 IOptions

IOptions如果我们可以没有它,为什么我们需要它?

IOptions

interface 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)

c# dependency-injection

9
推荐指数
1
解决办法
1443
查看次数