Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access from class library to appsetting.json in Asp.net-core

I am trying to access appsetting.json file from a class library. So far the solution that I found is to create a configuration class implementing interface IConfiguration from Microsoft.Extensions.Configuration and add the json file to class and read from the same.

var configuration = new Configuration();
configuration.AddJsonFile("appsetting.json");
var connectionString= configuration.Get("connectionString");

This seems to be bad option as we have to add the json file each time we have to access the appsetting configuration. Dont we have any alternative like ConfigurationManager in ASP.NET.

like image 504
cpr43 Avatar asked Jul 15 '16 13:07

cpr43


People also ask

What is Appsetting json in ASP.NET Core?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.

How do I add Appsettings json in .NET Core console app?

Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.


3 Answers

I know an answer has already been accepted, but this questions is a top hit on Google and OPs question is about class libraries and not an ASP.NET Web App or a WebApi which is what the accepted answer uses.

IMO, class libraries should not use application settings and should be agnostic to such settings. If you need application settings in your class library, then you should provide those settings from your consumer. You can see an example of this On This SO Question

like image 130
Stephen P. Avatar answered Oct 06 '22 01:10

Stephen P.


I'm assuming you want to access the appsettings.json file from the web application since class libraries don't have an appsettings.json by default.

I create a model class that has properties that match the settings in a section in appsettings.json.

Section in appsettings.json

"ApplicationSettings": {
    "SmtpHost": "mydomain.smtp.com",
    "EmailRecipients": "[email protected];[email protected]"
}   

Matching model class

namespace MyApp.Models
{
    public class AppSettingsModel
    {
        public string SmtpHost { get; set; }
        public string EmailRecipients { get; set; }
    }
}

Then populate that model class and add it to the IOptions collection in the DI container (this is done in the Configure() method of the Startup class).

services.Configure<AppSettingsModel>(Configuration.GetSection("ApplicationSettings"));

// Other configuration stuff

services.AddOptions();

Then you can access that class from any method that the framework calls by adding it as a parameter in the constructor. The framework handles finding and providing the class to the constructor.

public class MyController: Controller
{
    private IOptions<AppSettingsModel> settings;

    public MyController(IOptions<AppSettingsModel> settings)
    {
        this.settings = settings;
    }
}

Then when a method in a class library needs the settings, I either pass the settings individually or pass the entire object.

like image 39
Clint B Avatar answered Oct 06 '22 00:10

Clint B


Besides the questions has an accepted answer, I believe that there is no one that applies to just a class library without having Startup projects or having dependencies with Asp.NetCore stack or IServiceCollection.

This is how I achieved to read the config from a class library:

using Microsoft.Extensions.Configuration;
using System.IO;

public class ConfigSample
{
    public ConfigSample
    {
            IConfigurationBuilder builder = new ConfigurationBuilder();
            builder.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json"));

            var root = builder.Build();
            var sampleConnectionString = root.GetConnectionString("your-connection-string");
    }
}

The following nuget packages are required:

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.FileProviders.Abstractions
  • Newtonsoft.Json
  • Microsoft.Extensions.Configuration.Json
like image 21
Carlos Fernández Avatar answered Oct 06 '22 01:10

Carlos Fernández