Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog post about shared appsettings #54

Closed
tincann opened this issue Jul 2, 2019 · 2 comments
Closed

Blog post about shared appsettings #54

tincann opened this issue Jul 2, 2019 · 2 comments
Assignees

Comments

@tincann
Copy link

tincann commented Jul 2, 2019

Hi Andrew,

In this blogpost you're talking about sharing appsettings accross multiple projects.

.ConfigureAppConfiguration((hostingContext, config) =>
{
    var env = hostingContext.HostingEnvironment;

    var sharedFolder = Path.Combine(env.ContentRootPath, "..", "Shared");

    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();
})

The behavior I observe when setting the Build action and Copy to Output directory properties of my SharedSettings.json file to Content and Copy if newer, respectively, is that dotnet run copies the SharedSettings.json to the build directory as expected. Therefore, it is not needed to specify a path (as seen below) to the relative location of SharedSettings.json.

.AddJsonFile(Path.Combine(sharedFolder, "SharedSettings.json"), optional: true) // When running using dotnet run

This will work for local development, as well post publish. Is this correct? Or am I missing something?

Thanks!

@tincann
Copy link
Author

tincann commented Jul 2, 2019

I understand now. The above is true for Console apps, but not for Web apps where the ContentRoot is not equal to the build directory. I fixed it with this:

        private static IConfigurationBuilder AddSharedConfig(this IConfigurationBuilder config, IHostingEnvironment env) {
            var appSettingsPath = "appsettings-shared.json";

            if(env.EnvironmentName == "Development") {
                appSettingsPath = Path.Combine(env.ContentRootPath, "..", "appsettings-shared.json");
            }

            return config.AddJsonFile(appSettingsPath, optional: false, reloadOnChange: true);
        }

The advantage here is that you can set optional to false, which will give an error if the file can't be loaded.

@tincann tincann closed this as completed Jul 2, 2019
@andrewlock
Copy link
Owner

Yeah, you got it 🙂 if that works for you, great! I wasn't able to use it that approach, as people run environments locally in multiple ways, so can't make those assumptions based on environment🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants