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

zai*_*man 32 c# .net-core .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核心标记.

小智 34

正如杰霍夫所说,似乎没有变化.

正如Jeroen Mostert所说,ConfigurationBuilder在它自己的包中.

但请确保您还有.AddJsonFile()扩展所在的Microsoft.Extensions.Configuration.Json包.

总之,您需要以下两个NuGet包:

  • Microsoft.Extensions.Configuration(2.0.0)
  • Microsoft.Extensions.Configuration.Json(2.0.0)

  • 我通过在 VS2017 中为文件启用“复制到输出目录”设置来使其工作。我将其设置为“始终复制”。.csproj 现在有这个:`<ItemGroup> <None Update="appsettings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> </ItemGroup>` 并且它完美地工作。 (2认同)

The*_*his 10

private static IServiceProvider provider;在您的Program.cs中存储a .然后像在aps.net核心中一样设置配置,但当然你会在Main()中进行.然后配置IServiceProvider中的每个部分.这样您就可以使用构造函数依赖注入.另请注意,我的淡化示例中有两个配置.一个包含应该保留在源代码控制之外并存储在项目结构之外的秘密,我有AppSettings,其中包含不需要保密的标准配置设置.(这很重要!)

如果要使用配置,则可以将其从提供程序中取出,或将对象从提供程序中拉出来,这些提供程序在其构造函数中使用您的设置类.

    private static IServiceProvider provider;
    private static EventReceiver receiver;
    static void Main(string[] args)
    {
        IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(path: "/opt/Secrets.json", optional: false, reloadOnChange: true)
            .AddJsonFile(path: "AppSettings.json", optional: false, reloadOnChange: true)
            .Build();
        provider = new ServiceCollection()
            .AddSingleton<CancellationTokenSource>()
            .Configure<Settings>(config.GetSection("SettingsSection"))
            .BuildServiceProvider();
        receiver = new EventReceiver<Poco>(ProcessPoco);
        provider.GetRequiredService<CancellationTokenSource>().Token.WaitHandle.WaitOne();
    }

    private static void ProcessPoco(Poco poco)
    {
        IOptionsSnapshot<Settings> settings = provider.GetRequiredService<IOptionsSnapshot<Settings>>();
        Console.WriteLine(settings.ToString());
     }
Run Code Online (Sandbox Code Playgroud)

这些是我建议在制作dotnetcore cli应用时开始的依赖项.

<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="microsoft.extensions.logging.abstractions" Version="2.0.0" />
Run Code Online (Sandbox Code Playgroud)

另请注意,您需要将设置复制到发布和构建目录.您可以通过向csproj文件添加目标来实现.

  <Target Name="CopyToOut" BeforeTargets="BeforeBuild">
    <Copy SourceFiles="../ProjPath/AppSettings.json" DestinationFolder="$(TargetDir)" SkipUnchangedFiles="true" />
  </Target>
  <Target Name="CopyToOutOnPublish" AfterTargets="Publish">DestinationFolder="$(PublishDir)" SkipUnchangedFiles="true" />
    <Copy SourceFiles="../ProjPath/AppSettings.json" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="true" />
  </Target>
Run Code Online (Sandbox Code Playgroud)