Sampath
Sampath

Reputation: 65978

Access appsettings.json file setting from the class library project

I know how to set appsettings.json file on web project.But can you tell me how to access that from the class library project ?

I can configure it as shown below on the web project.Then how can I access that values from the class library project ? Is that possible ?

public class MyConfig
{
    public string ApplicationName { get; set; }
    public int Version { get; set; }
}

public class Startup 
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

        Configuration = builder.Build();
    }
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    // Add functionality to inject IOptions<T>
    services.AddOptions();

    // Add our Config object so it can be injected
    services.Configure<MyConfig>(Configuration);
}

public class HomeController : Controller
{
    private readonly IOptions<MyConfig> config;

    public HomeController(IOptions<MyConfig> config)
    {
        this.config = config;
    }

    // GET: /<controller>/
    public IActionResult Index() => View(config.Value);
}

Upvotes: 4

Views: 9154

Answers (1)

Ahmar
Ahmar

Reputation: 3877

You need to get the section and then bind it, change this

// Add our Config object so it can be injected
services.Configure<MyConfig>(Configuration);

TO

// Add our Config object so it can be injected
services.Configure<MyConfig>(option => Configuration.GetSection("MyConfig").Bind(option));

Then you will get values in HomeController or any other place where you inject this.

public class HomeController : Controller
{

    private readonly IOptions<MyConfig> config;

    public HomeController(IOptions<MyConfig> config)
    {
        this.config = config;
    }

    public IActionResult Index()
    {
        var value = config.Value.ApplicationName;
        var vars = config.Value.Version;
        return View();
    }
}

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "MyConfig": {
    "ApplicationName": "My Application Name",
    "Version": 1
  }
}

enter image description here

How we call this other then web project ?

Asp.Net Core DI resolve all dependencies before creating controller. So,we can create an interface and implement it. Then We can inject it into controller and from there access the appsettings.json settings.

ICategory.cs

public interface ICategory
{
    IOptions<MyConfig> config;
    IQueryable<Categories> Get();
}

Category.cs

public class Category : ICategory
{
       private readonly IOptions<MyConfig> config;

       public Category(IOptions<MyConfig> config)
       {
            this.config = config;
       }
       public Categories Get(long id)
       {
            var value = config.Value.ApplicationName;
            var vars = config.Value.Version;
            return category;
      }
}

CategoryController.cs

public CategoriesController(ICategory category)
{
   this.category = category;
}

When we call category.Get() method we can access the appsettings.json settings in that method.

Register it in ConfigureServices method of Startup.cs

services.AddTransient<ICategory, Category>();

Upvotes: 3

Related Questions