4

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);
}

1 Answer 1

3

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>();
Sign up to request clarification or add additional context in comments.

8 Comments

can you show me how to inject it into another project's class file ? Yes we can do that it within the web project as you shown.But how can I do it withing another class library project ? under same solution where I have multiple class library projects.
I added an example please check.
@Ahmar...IOptions<MyConfig> config;, in the ICategory interface -- error...interfaces cannot contain fields
@Ahmar... That interface is in my class library project and the DatabaseSettings class is in that project too.
This does not explain how to inject an interface in a totally different project in the same solution.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.