Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading appsettings.json from .net standard library

i have started a new RESTful project using .NET Core Framework.

I divided my solution in two parts: Framework (set of .NET standard libraries) and Web (RESTful project).

With Framework folder i provide some of library for furthers web project and into one of these i'd like to provide a Configuration class with the generic method T GetAppSetting<T>(string Key).

My question is: how can i get the access to the AppSettings.json file in .NET Standard?

I have found so many example about reading this file, but all these examples read the file into the web project and no-one do this into an extarnal library. I need it to have reusable code for Others project.

like image 790
Lollo Avatar asked May 28 '18 09:05

Lollo


People also ask

How do I get Appsettings development json in Visual Studio?

In Visual Studio use launchSettings. json or use Porject->Properties->Debug->Enviornment Variable to set the environment for debugging purposes.

Where can I find Appsettings json?

appsettings. json is one of the several ways, in which we can provide the configuration values to ASP.NET core application. You will find this file in the root folder of our project. We can also create environment-specific files like appsettings.


Video Answer


2 Answers

As already mentioned in the comments, you really shouldn't do it. Inject a configured IOptions<MyOptions> using dependency injection instead.

However, you can still load a json file as configuration:

IConfiguration configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory()) // Directory where the json files are located
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .Build();

// Use configuration as in every web project
var myOptions = configuration.GetSection("MyOptions").Get<MyOptions>();

Make sure to reference the Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Json packages. For more configuration options see the documentation.

like image 133
Bruno Zell Avatar answered Oct 06 '22 06:10

Bruno Zell


I extended this scenario in order to manage also (optional) User Secrets (from package: Microsoft.Extensions.Configuration.UserSecrets):

IConfiguration configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory()) // Directory where the json files are located
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddUserSecrets(Assembly.GetEntryAssembly(),optional:true);
    .Build();

The order of adding Json Files and User Secrets is important here. See Calling AddJsonFile and AddUserSecrets

I fully agree this is not the preferred way (instead use IOptions<> and Dependency Injection and let the application configure the library). But I am mentioning this because I was working on an (very old) library which was reading from app.config (xml). This library can't be configured from the application, instead the library does it directly (expecting values in app.config). This library is used for Full Framework, .NET Core and .NET5 (or newer) applications now. So I had to support appsettings.json as well. It was not really possible to adapt the library in a way so that the application can provide the necessary configuration values to the library. Because of that I added support for JSON to it (for the time being - maybe later on we can spend more effort to make it configureable from the application)

Finally I also support environments and my code looks like this:

var builder = new ConfigurationBuilder()
                      .SetBasePath(Directory.GetCurrentDirectory())
                      .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
            
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (!string.IsNullOrEmpty(environment))
{
    builder = builder.AddJsonFile(string.Format("appsettings.{0}.json", environment), optional: true, reloadOnChange: false);
    if (string.Equals(environment, "Development", StringComparison.CurrentCultureIgnoreCase))
    {
        builder = builder.AddUserSecrets(Assembly.GetEntryAssembly(),optional:true);
    }
}

Note that I decided to manage User Secrets only for Development scope.

like image 2
Thomas Purrer Avatar answered Oct 06 '22 06:10

Thomas Purrer