On Understanding ConfigurationManager in .NET - c#

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.

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.

Correct, ConfigurationManager looks to the executing assembly for the app.config or web.config to lookup settings.
So if your class library DLL that accesses configuration settings is running under a web site project then the app settings will need to be added to web.config, not the app.config of the class library.

Related

How to Include an App.Config file of a Class Library in the Web Publish release package

I have ran into a scenario where I have added an App.Config file in a Class Library which I am referencing from my ASP.NET MVC project.
Now while publishing my Web project I can only see the DLL of the class library in the Bin folder but not the App.Config of the same. Is there any way to include that in the Web publish.
Thanks
You need to tell the compiler to copy these files to the output directory. To do so, select the app.config from the solution explorer and hit F4 for properties and select Copy Always like the below example
Your class library shouldnt be using the app.config file. Instead the driver application or consuming application should be having a single app.config and should feed this dll with parameters.
This will also help your application being more SOLID. Your class library should be doing single job. Reading from a app.config file adds a second responsibility.
Also, class library not reading from the app.config file make your class library loosely coupled and it can be taken anywhere.

External dll is referencing current project's web.config

I've got two projects, WebAPI project as A and MVC4 project as B.
The dll of A project is referenced in B project.
I found that A's dll is using the config setting in B's web.config.
These projects have different definition for corresponding web.config.
So how can I do A's dll to use its own web.config and not to use B's web.config?
The configuration file is read only from starter project. You can merge configs if they differ in appsettings or connectionstrings. But if they differ in system settings like .net version, authentication and so on, you should launch your projects separately.
You're doing strange things, because MVC4 has initial support for WebApi. You can extract business logic from WebApi into another project. Do you really need to have WebApi controllers in separate project?

How to use class library in separate project within the same MVC4 solution?

So I'm trying to access a couple of IsInteger/IsNumeric functions from a custom class library I wrote in a separate project but within the same MVC4 solution.
I have a class library called MyHelpers (class library project) and the other is called Portal (a web application project), so two separate projects in the same MVC4 solution. After building MyHelpers successfully and putting the .dll in the Portal project's bin folder AND adding a reference to MyHelpers.dll, I still cannot find a reference to it in my Controller .cs file when adding the "using MyHelpers;" statement.
Why is this happening? Am I missing something? I thought that if I add a reference to the .dll and use the "using" statement it will let me reference the functions in the class library. What gives?
Please help and thanks much in advance.
In your MVC4 project, add a reference to your MyHelpers project. This way when you build MyHelpers again, your MVC4 project will have the most updated build of the MyHelpers.dll in the bin directory. You should not simply just reference the dll directly unless you have changed your output directory to the MVC4 project's bin directory. Be sure to check your output directories and make sure that all necessary dlls are in the directory of your MVC4 application.

When I build a c# project where does the service reference code go

when I build a project/solution that has a service reference in it, where is its compiled code?
If I have a project called JR.Rowing.Utilities that has an output of class library, the \bin directory contains:
JR.Rowing.Utilities.dll
JR.Rowing.Utilities.dll.config
JR.Rowing.Utilities.dll.xml
JR.Rowing.Utilities.pdb
Is the code for the service reference contained in one of these four files?
If I have another -already built- project that depends on calling on a new version of the service reference found in this one can I just copy these dll files over the old ones of this project?
Thanks!
The compiled code for the service reference will be in the JR.Rowing.Utilities.dll file. The configuration generated by adding the service reference will go into your App.config file which, when compiled, ends up being your JR.Rowing.Utilities.dll.config file.
The JR.Rowing.Utilities.pdb is your debug file which is generated because your project is being built using the Debug configuration.
No it is not in the files you have mentioned.
There is separate folder and separate tree view item in solution explorer for service reference.

What happens to the app.config file once I reference the class library

I have a class library with a app.config file with 4 settings in it. Previously when I tried this (VS2008) as soon as I referenced the library I would not be able to access the config file/application would break, but for some reason it works now. I have referenced the library in 3 applications and I can still access the settings fine but the actual config file is missing.
Does the config file get compiled along with the library as soon as you reference it or what happens to it?
(working in vs2010)
It probably works because there are defaults in the generated Settings.Designer.cs file. The more important question would be: How can I change my settings?
And that would require copying or merging the relevant XML sections from the library app.config to that of the referencing application.
The config file is renamed; "app" is replaced by the name of your assembly. So if your app.config is associated with a program, say Foo.exe, then in your output folder (eg bin\debug) you should have a Foo.exe.config. If your app.config is associated with a class library, say Bar.dll, then in the output folder it will be Foo.dll.config.

Categories