Solved

Help with app.config in C# program

Posted on 2014-12-13
7
377 Views
Last Modified: 2014-12-13
Hi, I am trying to merge common content external content into a single app.config. I cannot get it to work. Here is what I do

1. Start/Visual Studio 2013/etc and select new Console Application project
2. My source code looks like shown below and is saved at c:\John\ConsoleApplication1 and directories underneath that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Open in new window


My project app.config file looks like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <linkedConfiguration href="file://C:\John\ConsoleApplication1\sharedConfig1.config"/>
  </assemblyBinding>

  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
   </startup>
</configuration>

Open in new window


3. I create a sharedConfig1.config file as referenced above and place it in c:\john\consoleapplication1. Its contents look like this

<test1>
   <test2>
    <add key="test3" value="test4" />
   </test2>
  </test1>

Open in new window


4. I compile and build the application. When I open up C:\John\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.config is contains this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <linkedConfiguration href="file://C:\John\ConsoleApplication1\test.config"/>
  </assemblyBinding>

  <startup> 
        <supportedRuntime version="v4.0" sk[url="http://msdn.microsoft.com/en-us/library/ms404300(v=vs.110).aspx"]http://msdn.microsoft.com/en-us/library/ms404300(v=vs.110).aspx[/url]u=".NETFramework,Version=v4.5" />
   </startup>
</configuration>

Open in new window



My question is why? Why doesn't it include the information in C:\John\ConsoleApplication1\sharedConfig1.config

According to http://msdn.microsoft.com/en-us/library/ms404300(v=vs.110).aspx I have done everything correctly. According to the MSDN link, it says "All linked configuration files are merged to form one file, similar to the behavior of the #include directive in C/C++". This is exactly what I want I just cannot get it to work.

All help or even ideas really welcome.

Thank you

John
0
Comment
Question by:John Bolter
  • 4
  • 2
7 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40497863
In Object Browser, select to see what is "Copy to output directory" property when App.config is selected. The options are:

- never copy
- copy if newer
- always copy

If you select one of the last two, App.config should be copied to your Debug folder. Visual Studio never changes the original files (as far as I know). It changes them in compile time if directed to do so.

Mike
0
 

Author Comment

by:John Bolter
ID: 40497869
HI, thanks, but that doesn't merge the XML config files which is what I want.
Basically I want the two XML config files to be merged like #include like is says in the MSDN documentation.
Does this not work
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40497884
It does that on run-time I believe  and puts in Debug folder. After you run, check your Debug folder to see if the combined App.config is there.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:John Bolter
ID: 40497892
Hi eghtebas, it can't do it at runtime because all the source code config files for merging aren't deployed. I have tested it too, and unless my code is bugged, the configuration files defined in the shared file aren't there.

Also according to this MSDN link http://msdn.microsoft.com/en-us/library/ms404300(v=vs.110).aspx it says All linked configuration files are merged to form one file, similar to the behavior of the #include directive in C/C++.

I just don't think it works. I have googled a lot and the only hits I get are other people not getting this to work either :-( Some people talk of something named fuslogvw and fusion only recognising this linkedConfiguration syntax, but that isn't written on the Microsoft website.
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40498012
John, I believe that the MSDN TID is a little misleading (actually a lot misleading) which is causing a misunderstanding on the intended use for this element.

The line that needs to be scrutinized is this:
The settings in included configuration files only affect loader binding policy and are used only by the loader. The included configuration files can have settings other than binding policies, but those settings do not have any effect.
This primary usage for this is for versioning control (or versioning redirection).

For testing purposes:

1. Create a new C# console project (name it what you like, I called mine EE_Q28580184).

2. Add an App.config and LinkedConfiguration.xml.

3. Place the following in the App.config; make sure you change the path to reflect the location of the LinkedConfiguration.xml.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <linkedConfiguration href="file://<pathtolinkedconfiguration.xml>\LinkedConfiguration.xml"/>
   </assemblyBinding>
   <runtime>
   </runtime>
</configuration>

Open in new window

4. Place the following in LinkedConfiguration.xml; you will be adding a public key token shortly.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="HelloWorld" publicKeyToken="<placetheassemblypublickeytokenhere>"/>
            <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

Open in new window

5. Place the following in the Program.cs; disregard the error with regards to the HelloWorld name not existing in the current context.

using System;

namespace EE_Q28580184
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine(HelloWorld.Console.Write);
			Console.ReadLine();
		}
	}
}

Open in new window

At this point your project should look like this:Capture.JPG

6. Click on File and choose Add -> New Project

Choose the project type for a Class Library, Name this project HelloWorld and select the Add the project to the current solution:Capture.JPG

7. Rename the Program.cs for HelloWorld to Console.cs and add the following code to it

namespace HelloWorld
{
	public class Console
	{
		public static string Write
		{
			get { return string.Format("Hello World from version {0}", typeof(Console).Assembly.GetName().Version); }
		}
	}
}

Open in new window

8. Right-click on the HelloWorld project and choose properties.

Capture.JPG

9. Locate the Signing tab on the right, click to 'Sign the assembly', choose <New...> from the 'Choose a strong name key file' dropdown.

Capture.JPG

10. Type in a key file name (I used EE_Q28580184Strong) and either put in a password and confirm it or uncheck the Protect my key file with a password option and press Ok.

Capture.JPGCapture.JPGAt this point your Entire solution should look like this:Capture.JPG

11. Now add a reference in the EE_Q28580184 project to the HelloWorld project and set the Copy Local property to False for the HelloWorld reference.

Capture.JPGCapture.JPGCapture.JPGCapture.JPG

12. Right click on the HelloWorld project and choose build.

Capture.JPG

13. Create a folder on the root of your C: drive called HelloWorld and copy the HelloWorld.dll to the HelloWorld folder.

14. Open the Visual Studio command prompt and type the following command - gacutil /i C:\HelloWorld\HelloWorld.dll and press enter.

Capture.JPGCapture.JPG

15. Change the Assembly version on the HelloWorld from 1.0.0.0 to 2.0.0.0 and rebuild the HelloWorld project.

Capture.JPG

16. Repeat steps 13 and 14 to place the HelloWorld version 2.0.0.0 dll into the GAC (Global Assembly Cache).

17. Open Windows Explorer and go to C:\Windows\assembly, locate HelloWorld and copy the public key token

Capture.JPG

18. Put the public key token in the LinkedConfiguration.xml.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="HelloWorld" publicKeyToken="2e8643da96486837"/>
            <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

Open in new window

If everything up to this point has been completed properly, when you run the Project your should get the following results:Capture.JPGNow if you change your LinkedConfiguration.xml to contain the following -
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="HelloWorld" publicKeyToken="2e8643da96486837"/>
            <bindingRedirect oldVersion="2.0.0.0" newVersion="1.0.0.0"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

Open in new window

And rerun your application you get the following results -Capture.JPGHTH,

-saige-
0
 

Author Comment

by:John Bolter
ID: 40498021
Saige, thank you again, this single answer has made my subscription to Experts Exchange worth the money.

From the documentation on MSDN, it is misleading. Although I think I now understand the purpose of <linkedConfiguration> from your feedback, I can also see to it isn't what I am looking.

Thank you!
0
 

Author Closing Comment

by:John Bolter
ID: 40498022
Thank you!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now