0

I am working on ASP.NET Core 6.0 WebAPI and I need to get data from a CRM system (Sales Logix) using Sage SData API. We have different CRM environments (Production, Staging, Development) and I want to be able to connect (or test) any environment from my WebAPI project.

For that to work, I would like to add a configuration key (to indicate a particular CRM environment) either in appsettings.json (or launchsetting.json). For example when setting is "crmEnvironment": "Development", I want to include a custom json file, named crm-dev.json. Similarly for "crmEnvironment": "Staging", I want to include crm-staging.json.

Each custom json file ideally contains the CRM Url, Username and Password.

Please tell me how can I conditionally add json config files as mentioned above, or is there any better approach of achieving similar results, considering security in mind. Best if I could have custom config files encrypted without having to shift away from the standards, just like we did for Web.Config files by inheriting ProtectedConfigurationProvider.

My questions is similar to How can I add a custom JSON file into IConfiguration?. But since Program.cs no longer uses the Main() method, I am wondering what would be the correct way to add custom json config files.

Talha Anwer
  • 123
  • 2
  • 8
  • Is this the scenario: You build the configuration using a certain set of JSON files, but then one of those files tells you which other files you need, so you need to rebuild the configuration? – Scott Hannen Dec 30 '21 at 21:07
  • @scott-hannen No, I just want to add just one json file at any time from my main config file (appsettings.json) – Talha Anwer Dec 30 '21 at 21:25
  • Try to use `IOptionsSnapshot` which can make it update on file change without having to restart the app. – Jason Pan Dec 31 '21 at 01:54
  • I guess you have a different appsettings for each enviroment too. Why don't use it. – Serge Dec 31 '21 at 11:20

1 Answers1

0

If this may work, I use this approach.

.AddJsonFile($"Config\\appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)

And I will have the Json file created for each environment. You can set the environment variable value either via IHost context or may be, simplifying your need by using Environment variable values from your respective environment.

Ak777
  • 346
  • 7
  • 18