为什么启动的IConfigurationRoot为空? [英] Why Startup's IConfigurationRoot is null?

查看:85
本文介绍了为什么启动的IConfigurationRoot为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将命令行参数传递给 Startup 类。遵循这个示例,我修改了我的 Program 类,它看起来像这样:

I'm trying to pass command line arguments to Startup class. Following this example, I modified my Program class and it looks like this:

        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("generalsettings.json", optional: false, reloadOnChange: true)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .AddCommandLine(args);  
        var config = builder.Build();

        var host = new WebHostBuilder()
            .UseUrls("http://*:5000")
            .UseConfiguration(config)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();

generalsettings.json 包含以下数据:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

因此,我注释掉默认的启动类的构造函数。然后我注意到这是分配 IConfigurationRoot配置的位置,因此,当我尝试从 ConfigureServices 使用它时它为空。我正在尝试使用程序 Main 内置的配置。我在这里想念什么?

Therefore I commented out default Startup class' constructor. Then I noticed that that's where IConfigurationRoot Configuration is assigned, and therefore, when I'm trying to use it from ConfigureServices it's null. I'm trying to use (I think) configuration built in Program's Main. What am I missing here?

更新

要明确:我我正在尝试在启动类中使用 args

To make it clear: I'm trying to use args in Startup class.

推荐答案

事实证明,在 GitHub Aspnet托管回购问题区域。 GitHub存储库始终是解决诸如此类的有趣问题的好地方。

It turns out that there has been a fair amount of discussion about this sort of thing in GitHub Aspnet hosting repo issues area. The GitHub repositories are always a good place to look on an interesting problem like this one.

为您总结一下,不要在Program.Main中设置IConfigurationRoot,在启动中进行设置。您可以像这样从Program.cs将命令行参数传递给Startup构造函数:

To sum things up for you, do not set up IConfigurationRoot in Program.Main, set it up in Startup. You can pass the command line arguments to the Startup constructor from Program.cs like so:

    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .ConfigureServices(services => services
                .AddSingleton(new ConsoleArgs(args))
            )
            .Build();
        host.Run();
    }

其中ConsoleArgs是持有人类,您可以创建自己的外观,如下所示:

where ConsoleArgs is a holder class you can create yourself that looks like:

public class ConsoleArgs
{
    public ConsoleArgs(string[] args)
    {
        Args = args;
    }
    public string[] Args { get; }
}

很明显,配置ConsoleArgs service 是键。这将允许它被注入到Startup构造函数中。然后您的Startup类构造函数看起来像

Obviously, configuring a ConsoleArgs service is the key. This will allow it to be injected into the Startup constructor. Then your Startup class constructor looks something like

public Startup(IHostingEnvironment env, ConsoleArgs args)
{
     var builder = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("generalsettings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables(prefix: "ASPNETCORE_")
    .AddCommandLine(args.Args);  
    var config = builder.Build();
}

此处的模式是配置托管环境程序,在启动时配置应用程序

The pattern here is "configure you hosting environment Program, configure you application in Startup"

这篇关于为什么启动的IConfigurationRoot为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆