Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with app.config in C# program

Posted on 2014-12-13
7
Medium Priority
?
514 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 34

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 34

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 34

Accepted Solution

by:
it_saige earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

688 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