1

I have a solution with two projects:

  • ASP.NET Core 6 Web API Project
  • Class Library Project

I want to access the appsettings.josn file in the web api project from within the class library.

The api project already has a reference to the class library (Request/Response Models, Services, Handlers...etc).

Adding a reference for the api project to the class library would create a circular reference problem.

What's the best solution for this problem? Do I ditch the 2-project solution structure and place everything under 1 project?

Taher Elhouderi
  • 233
  • 2
  • 11
  • the simpliest way is to configure DI in web project and inject `IConfiguration` object into class library. You can extend this method to use `IOptions` – Yehor Androsov Mar 29 '22 at 18:13
  • Access when? In regard to the question in the title, Right Click API Project > Add > Existing Item... and browse to the class project's appsettings.json. But I'm not sure what you mean. There is no reason to move to a single project for this issue alone. The API project (the executable project) should be in charge of most of the runtime configuration (connection strings and such). That's not to say that DLL settings are always a bad idea but do consider the scope of each setting. – rfmodulator Mar 29 '22 at 18:54
  • @rfmodulator In the class library, I have some methods that behave differently during runtime depending on certain values in appsettings. Does the above method allow me to use Configuration.GetSection like in the main webapi project, or do I have to parse it like any normal json file in the class library? – Taher Elhouderi Mar 29 '22 at 19:46

2 Answers2

2

We do not recommend using the ConfigurationManager Class in the class library to get the value of appsettings.json.

Reason:

Keep your class library flexible,don't do this in your class library. And Dependency Injection becomes a very flexible and powerful tool.

Best Practise

How to use the IOptions pattern for configuration in ASP.NET Core RC2

Test Steps And Result

Program.cs

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

builder.Services.AddOptions();

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationSettings": {
    "SmtpHost": "aa",
    "EmailRecipients": "bb"
  }
}

Model and test method in ClassLibrary.

enter image description here

Test Method in Controller

public string GetValue()
{
    GetValue g = new GetValue();
    string result = g.fortest(_settings.Value.SmtpHost,_settings.Value.EmailRecipients);
    return result;
}

enter image description here

Test Result

enter image description here

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
-2

Make a get function that return information that you want from project that is owner of json settings

  • Respectfully, this isn't a great answer - not only is it vague, but the solution you imply ignores the issue in the question of "Adding a reference for the api project to the class library would create a circular reference problem." – Luke Aug 04 '22 at 20:52