I'm trying to understand ConfigurationManager in .NET by practicing it in different scenarios.
I have two projects: Project1 and Project2. Project2 uses Project1.
My Situation: I have a section(serializedfilename) in my app.config file in project1. and I have this line of code in a class1 of project1
private static string SerializedConfiguration = ConfigurationManager.AppSettings["SerializedFilename"];
In project2, I call a method in the class1 of project1 and i'm getting an exception.
its like .NET doesnt seem to be able to find the settings in app.config file of project1 when called from project2 even though i'v referenced project1 appropriately.
However, when i move the section(serializedfilename) to app.config file of project2...everything seems to work well...
Can someone explain to me what is happening here? I'm tempted to assume that ConfigurationManager only exist in the context of the client application(in this case project2) and not in the original application(project1 in this case)
Note: i'm new(1month+ to .NET)
Thanks.
ConfigurationManager is the class which helps to read data from configurations. Provides access to configuration files for client applications. Namespace: System.Configuration. To use the ConfigurationManager class, your project must reference the System.
ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.
ConfigurationManager Class (System. Configuration) Provides access to configuration files for client applications.
var builder = WebApplication. CreateBuilder(args); // Add services to the container. ... ConfigurationManager configuration = builder. Configuration; // allows both to access and to set up the config IWebHostEnvironment environment = builder.
When you add a reference app.Config is not copied. App.config needs to be in your primary project, in your case project 2. if you add it in project 2 and call it in the referenced object Project 1, you will not get any error as VS will pick it from your primary project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With