1

In .NET framework i usually store key value pipeline variables in a app.config file and access those key values by doing the following

<configuration> <appSettings> <add key="BaseURL" value=""/> <add key="Env" value=""/> <add key="API_KEY" value=""/> </appSettings> </configuration> 
 public static class PipelineVariables { public static string BaseURL => ConfigurationManager.AppSettings["BaseURL"]; public static string Environment => ConfigurationManager.AppSettings["Env"]; public static string APIKEY => ConfigurationManager.AppSettings["API_KEY"]; } 

Just for fun i want to do a API test automation project using Visual Studio Code and .Net Core

I created the following appsettings.json

{ "AppSettings": { "BaseURL": "", "Env": "", "API_KEY": "" } } 

Just wondering whats the recommended way to access these key values? I will use these values as pipeline variables in which i will store in Azure. I want to access the key values to pass into my code.

***UPDATE: I got it working similar to the way i do it in .NET Framework projects

I would like the be able to use switch statement on these variables for use in Azure for CI/CD, hence why i want separate methods for variable

However if the a way to simplify the below code?

 public static string APIKey => new ConfigurationBuilder() .SetBasePath(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName) .AddJsonFile("appsettings.json", true, true).Build().GetSection("AppSettings:API_KEY")?.Value; public static string BaseURL => new ConfigurationBuilder() .SetBasePath(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName) .AddJsonFile("appsettings.json", true, true).Build().GetSection("AppSettings:BaseURL")?.Value; 

3 Answers 3

1

It depends on which type application you run. You can solve it in a multiple way.

For example in Dotnet Core API in the startup class (This is often already auto generated in a API or MVC project):

public IConfiguration Configuration { get; } public Startup(IConfiguration configuration) { Configuration = configuration; } public void ConfigureServices(IServiceCollection services) { string apiKey = Configuration.GetSection("AppSettings:API_KEY")?.Value; } 

But you can for example run it also in an console application or different kind project:

public void Load() { var builder = new ConfigurationBuilder() .SetBasePath(Path.Combine("baseDirectory")) .AddJsonFile("appsettings.json", true, true) .AddEnvironmentVariables(); var config = builder.Build(); string apiKey = config["AppSettings:API_KEY"]; } 

Add the following nuget packages to make the code from above work:

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Binder
  • Microsoft.Extensions.Configuration.EnvironmentVariables
  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json

***UPDATE: Maybe this helps you to simplfy the code:

public class PipelineVariables { public string BaseURL { get; set; } public string Environment { get; set; } public string APIKEY { get; set; } private static PipelineVariables pipelineVariables; public static PipelineVariables Get() { if (pipelineVariables == null) { var builder = new ConfigurationBuilder() .SetBasePath(Path.Combine("baseDirectory")) .AddJsonFile("appsettings.json", true, true) .AddEnvironmentVariables(); var config = builder.Build(); pipelineVariables = new PipelineVariables { APIKEY = config["AppSettings:API_KEY"], BaseURL = config["AppSettings:BaseURL"], Environment = config["AppSettings:Env"] }; } return pipelineVariables; } } 

Or a more direct solution is get the config class this way:

 pipelineVariables = config.GetSection(nameof(PipelineVariables)).Get<PipelineVariables>(); 

Only the names of variables in the class needs to be same as in the appsettings.json

The appsettings.json will became this way:

{ "PipelineVariables": { "BaseURL": "", "Environment": "", "APIKEY": "" } } 

Or you just change the properties and the class name in C#.

Example to get the settings:

public void RunPipeline() { PipelineVariables variables = PipelineVariables.Get(); Console.WriteLine(variables.BaseURL); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. It was very helpful in me trying to solve this. I added an update the my original post to see if i can simplify my current solution
@IOF I just updated my answer. Maybe it helps you to simplify your answer.
0

in asp .net core api you can use this solution :

1.Create your base AppSettings class with your properties :

public class AppSettings { public string BaseURL { get; set; } public string Env { get; set; } public string API_KEY { get; set; } } 

2.You get your section from appsettingjson in startup ctor :

 var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); this.Configuration = builder.Build(); var settings = Configuration.GetSection(nameof(AppSettings)).Get<AppSettings>(); 

3.Now you get your settings from appsettingjson in "settings" instance and you can use it in your statics feild

Comments

0

I used the Controller Class to get the aspsetting.development variable appsettings.development.json "OpenAI": { "ApiKey": "sk-" },

 public String _apiKey; public IConfiguration Configuration { get; } public LangChainController(IConfiguration configuration) { Configuration = configuration; var apiKey = Configuration.GetSection("OpenAI:apiKey"); _apiKey = apiKey.Value; } 

Comments