繁体   English   中英

appsetting.json如何在多个项目.net核心中工作?

[英]How appsetting.json works in multiple project .net core?

我需要对.net核心中的appsettings进行工作的指导或解释,如果每个人都有appsettings.json并且都具有连接字符串部分,则我有2个项目api和dataacess(classlib)。 当我从dataaccess项目中读取连接字符串时,它将从作为api的托管应用程序中读取值,因此,当我构建使用另一个也具有appsettings文件的classlib项目的项目时,appsettings的工作方式会在生成输出时进行合并还是托管应用程序覆盖classlib或classlib设置被完全忽略? 感谢您的指导

这是我如何使用ASP.Net Core的配置模式读取连接字符串的方法

您可以在这里阅读我的完整代码和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();
     }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM