Homepage image
Sample of the Day RSS Feed

Sample Download: http://code.msdn.microsoft.com/CSMergeConfig-6e731d92

Today’s code sample demonstrates a scenario where the config file of the dll needs to be merged with the Console application so that the Configuration of the class library (contains reference to service) can be accessed during application run to initialize the class. 

The sample is created by Microsoft Tech Lead: Ganesh Shankaran.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

This program is a generic sample for a scenario where the config file of the dll needs to be merged with the Console application so that the Configuration of the class library (contains reference to service) can be accessed during application run to initialize the class.

Purpose of the sample:

Merging the configuration file for a referenced managed dll with the console application such that the Specified Section from the config file of Class Library will be merged with the console application.

Why do we need to merge the config file?

By default, only the config file of the executable will be accessed during runtime. If we need to load a class Library that has a service reference, the respective dll.config file cannot be accessed which will result in a System.Reflection.TargetInvocationException. So, to avoid there has been lot of request from our customers to provide a way to merge the config file such that we will be able to access the config file of the application and still be able to initialize.

 

Using the Code

We first load the two config files and then merge them.

var doc = XElement.Load(@"ServiceReferenceddll.dll.config"); 
  
var query = from ele in doc.Elements("system.serviceModel") 
            select ele; 
  
var doc1 = XElement.Load(@"..\CSMergeConfig\App.config"); 
  
var node1 = doc.Descendants("system.serviceModel").FirstOrDefault(); 
var node2 = doc1.Descendants("system.serviceModel").FirstOrDefault(); 
  
XNode node = doc1.Element("system.serviceModel"); 
  
// Lets iterate the node of the console until the last node is reached 
if (!XElement.DeepEquals(node1, node2)) 
{ 
    node2.Add(node1.Nodes()); 
    doc1.Save(@"..\CSMergeConfig\App.config"); 
} 

These are the inputs:

First config file :

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
        <bindings> 
            <basicHttpBinding> 
                <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00" 
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
                    useDefaultWebProxy="true"> 
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
                    <security mode="None"> 
                        <transport clientCredentialType="None" proxyCredentialType="None" 
                            realm="" /> 
                        <message clientCredentialType="UserName" algorithmSuite="Default" /> 
                    </security> 
                </binding> 
            </basicHttpBinding> 
        </bindings> 
        <client> 
            <endpoint address="http://localhost/WCFService/Service.svc" binding="basicHttpBinding" 
                bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService" 
                name="BasicHttpBinding_IService" /> 
        </client> 
    </system.serviceModel> 
</configuration> 

Second config file -- app.config of the Console application that needs to be merged with the specific node from the ServiceReferenceddll.dll.config

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
  <system.serviceModel> 
  
  </system.serviceModel> 
</configuration> 

Expected output -- App.config of consoleApplication has the following output:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
  <system.serviceModel> 
    <bindings> 
      <basicHttpBinding> 
        <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
          <security mode="None"> 
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> 
            <message clientCredentialType="UserName" algorithmSuite="Default" /> 
          </security> 
        </binding> 
      </basicHttpBinding> 
    </bindings> 
    <client> 
      <endpoint address="http://localhost/WCFService/Service.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService" name="BasicHttpBinding_IService" /> 
    </client> 
  </system.serviceModel> 
</configuration> 

Note:

When run the application for the first time, you will see that we will get an unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll, along with this error, the config will be merged. The application will run successfully post this.

 

More Information

http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx

http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.nextnode.aspx