Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Understanding ConfigurationManager in .NET

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.

like image 503
oliverdejohnson Avatar asked Jul 26 '13 15:07

oliverdejohnson


People also ask

What is ConfigurationManager C#?

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.

Can I use ConfigurationManager in .NET Core?

ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.

What is the use of ConfigurationManager Appsettings?

ConfigurationManager Class (System. Configuration) Provides access to configuration files for client applications.

How do I access net 6 config?

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.


1 Answers

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.

like image 172
Ehsan Avatar answered Sep 23 '22 10:09

Ehsan