简体   繁体   中英

Shared appsettings.json file between web and console VS project at .Net Core

I'm setting a appsettings.json file for shared between any projects .net Core (Web, Console and Library). I setup a shared file follow the step of this post https://andrewlock.net/sharing-appsettings-json-configuration-files-between-projects-in-asp-net-core/ , but i can't read file in console project.

this is the code that i have tried

    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("SharedSettings.json", optional: true, 
            reloadOnChange: true);

        IConfigurationRoot configuration = builder.Build();

        Console.WriteLine("Connection string: " + configuration.GetConnectionString("DefaultConnection"));
        Console.WriteLine("Hello World!");
        Console.Write("Press any key to continue...");
        Console.ReadKey(true);
    }

this is my project structure

在此处输入图片说明

in my project web the code for read file it is, i need read shared .json in the other's project.

  public static IWebHost BuildWebHost(string[] args) =>
     WebHost.CreateDefaultBuilder(args)
     .UseContentRoot(Directory.GetCurrentDirectory())
     .ConfigureAppConfiguration((hostingContext, config) => {
      IHostingEnvironment env = hostingContext.HostingEnvironment;

  // find the shared folder in the parent folder
  var sharedFolder = Path.Combine(env.ContentRootPath, "..", "Shared");

  //load the SharedSettings first, so that appsettings.json overrwrites it
  config
   .AddJsonFile(Path.Combine(sharedFolder, "SharedSettings.json"), optional: true) // When running using dotnet run
   .AddJsonFile("SharedSettings.json", optional: true) // When app is published
   .AddJsonFile("appsettings.json", optional: true)
   .AddJsonFile($ "appsettings.{env.EnvironmentName}.json", optional: true);

  config.AddEnvironmentVariables();
 })
 .UseStartup < Startup > ()
 .Build();

Without your ConfigurationBuilder implementation and how you are passing IConfiguration to your dependencies. Are they stand alone applications for instance?

For the other application, you can achieve your desired result by doing the following:

  1. On appsettings.json file in solution, open properties.
  2. Build to output directory.

When you are inside Visual Studio the application should read your appsettings.json without an issue. For my solution I created a directory called Configuration with two sub-directories Development and Production. Now when Visual Studio builds it should adhere to the above configuration mentioned, but to create a separate Build directory that is not apart of your bin I utilized a PowerShell script.

Param($configuration="Development")

if($configuration.ToUpper().StartsWith("P")) {
     $configuration="Production"
} else {
     $configuration="Development"
}

if((Test-Path .\Build)) {
     Remove-Item -r -fo .\Build
}

New-Item -ItemType Directory .\Build | Out-Null
dotnet publish --configuration Release --runtime win81-x64 --verbosity Quiet --self-contained true
Copy-Item -r .\bin\Release\netcoreapp2.1\win81-x64\publish\* .\Build\

Remove-Item .\Build\*.pdb
Remove-Item .\Build\appsettings.json

Copy-Item .\Configuration\$configuration\appsettings.json .\Build\
Copy-Item ".\Configuration\Deployment Resources\Script\*.ps1" .\Build\
Copy-Item ".\Configuration\Deployment Resources\Task\*.xml" .\Build\

echo "`n`n"
Write-Host "╭ ─────────────────────────── ╮"
Write-Host "         $configuration              "
Write-Host "╰ ─────────────────────────── ╯"
echo "`n`n"

To build, I navigate to my solution on command line and simply do: .\\Build.ps1 Production or .\\Build.ps1 and it will create a valid Build directory for me. You can ignore some of the other components, since they're for a scheduled task, install scripts, etc. that we also include so when we use a deploy script everything transfers to remote server.

I will modify answer once you provide more information about how your other projects are linked to your web solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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