• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

Help with app.config in C# program

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
John Bolter
Asked:
John Bolter
  • 4
  • 2
1 Solution
 
Mike EghtebasDatabase and Application DeveloperCommented:
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
 
John BolterAuthor Commented:
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
 
Mike EghtebasDatabase and Application DeveloperCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
John BolterAuthor Commented:
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
 
it_saigeDeveloperCommented:
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
 
John BolterAuthor Commented:
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
 
John BolterAuthor Commented:
Thank you!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now