2

I have my database related stuff in another project which is a class library. I would like to specify the connection string in my config.json file. However, I couldnt find out how to read the configuration. Inside the static constructor of my SessionFactory I call:

var builder = new ConfigurationBuilder()
.AddJsonFile("config.json");
Configuration = builder.Build();

However, I get an exception that the config file is not found since it's being searched in the executed application (which is a separate project). I tried to set the base path but no luck with that as well.

How do I read the config file in class library in a separate project ?

Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169

1 Answers1

2

This is the same as when previously using app/web.config. The configuration specific to your currently executing context should be local. If you want access to settings specified for a different project, you should instead add those settings to your current project's configuration.

For example, for the structure;

src
    DataProject
        config.json
    ApplicationProject
        config.json

Because you are executing ApplicationProject, all configuration needs to be specified in ApplicationProject\config.json, and then made available later to DataProject via dependency injection or otherwise. DataProject\config.json is irrelevant, because DataProject will not be the executing assembly.

If you have configuration values in DataProject\config.json that are neccessary when executing ApplicationProject, then you should copy those values into ApplicationProject\config.json.

Stafford Williams
  • 9,696
  • 8
  • 50
  • 101