C# appsetting.json如何在多个project.net core中工作?

C# appsetting.json如何在多个project.net core中工作?,c#,asp.net-core,C#,Asp.net Core,我需要关于appsettings如何在.net core中工作的指导或解释 如果每个项目都有appsettings.json,并且都有连接字符串部分,那么我有两个项目api和DataAccess(classlib)。当我从dataaccess项目中读取连接字符串时,它会从宿主应用程序(即api)中读取值,因此,当我构建一个使用其他classlib项目(也包含appsettings文件)的项目时,appsettings如何工作,是在生成输出时合并,还是宿主应用程序覆盖了classlib,或者完全忽

我需要关于appsettings如何在.net core中工作的指导或解释
如果每个项目都有appsettings.json,并且都有连接字符串部分,那么我有两个项目api和DataAccess(classlib)。当我从dataaccess项目中读取连接字符串时,它会从宿主应用程序(即api)中读取值,因此,当我构建一个使用其他classlib项目(也包含appsettings文件)的项目时,appsettings如何工作,是在生成输出时合并,还是宿主应用程序覆盖了classlib,或者完全忽略了classlib设置?感谢您的指导

以下是我如何使用ASP.Net核心配置模式读取连接字符串

您可以阅读我的完整代码和Microsoft

在我们开始做任何事情之前,请确保我们为控制台应用程序安装了2个包,因为我还想阅读appSetting.json中的一些设置

Install-Package Microsoft.Extensions.Options
Install-Package Microsoft.Extensions.DependencyInjection
在Program.cs文件中

public static void Main()
    {
        var serviceCollection = new ServiceCollection();
        ConfigureServices(serviceCollection);

        // create service provider
        var serviceProvider = serviceCollection.BuildServiceProvider();

        // entry to run app
        serviceProvider.GetService<WebJob>().RunQueue();
        serviceProvider.GetService<WebJob>().Run();
        Console.ReadLine();
    }

    private static void ConfigureServices(IServiceCollection serviceCollection)
    {
          var currentDir = ProjectPath.GetApplicationRoot();

         // build configuration
         varconfiguration = newConfigurationBuilder()
                   .SetBasePath(currentDir)
                   .AddJsonFile("appsettings.json",false)
                   .Build();

         serviceCollection.AddOptions();
         serviceCollection.Configure<WebJobSettings>(configuration.GetSection("WebJobSettings"));
         // add app
         serviceCollection.AddTransient<WebJob>();
    }

public class WebJob
{
  private readonly IOptions<WebJobSettings> _webJobSettings;
  public WebJob(
    IOptions<WebJobSettings> webJobSettings)
  {
    _webJobSettings = webJobSettings;
  } 
  public void Run()
  {


 GlobalConfiguration.Configuration.UseSqlServerStorage(_webJobSettings.Value.DbConnectionString); // here is how I access the config

     using(var server = new BackgroundJobServer())
     {
        Console.WriteLine("Hangfire Server started. Press any key to exit...");
        Console.ReadLine();
     }
  }
}
publicstaticvoidmain()
{
var servicecolection=新servicecolection();
配置服务(serviceCollection);
//创建服务提供商
var serviceProvider=servicecolection.BuildServiceProvider();
//运行应用程序的入口
serviceProvider.GetService().RunQueue();
serviceProvider.GetService().Run();
Console.ReadLine();
}
专用静态void配置服务(IServiceCollection serviceCollection)
{
var currentDir=ProjectPath.GetApplicationRoot();
//构建配置
varconfiguration=newConfigurationBuilder()
.SetBasePath(当前目录)
.AddJsonFile(“appsettings.json”,false)
.Build();
serviceCollection.AddOptions();
Configure(configuration.GetSection(“WebJobSettings”);
//添加应用程序
serviceCollection.AddTransient();
}
公共类WebJob
{
私有只读IOPS\u webJobSettings;
公共网络作业(
IOptions(网络作业设置)
{
_webJobSettings=webJobSettings;
} 
公开募捐
{
GlobalConfiguration.Configuration.UseSqlServerStorage(_webJobSettings.Value.DbConnectionString);//以下是访问配置文件的方式
使用(var server=newbackgroundjobserver())
{
Console.WriteLine(“Hangfire服务器已启动。按任意键退出…”);
Console.ReadLine();
}
}
}