Wednesday, April 25, 2012

Load assemblies with different versions

Basically we can load only the same version of assemblies into the application. But i need the requirement like , i have one WPF application and also having Class library. My class library will be having the version 5.0 of assemblies and my exe project will be having the 3.0 version of assemblies.
To execute the above requirement , we should do the following steps very carefully.

1. Add app.config file in the application:-

When we create the WPF application by default we will not be having app.config file in the application as like in the Asp.Net project. But we can achieve this by doing the couple of steps.
i) Right click in the project properties and choose ApplicationConfiguration file and the name should be App.config [ not App1.config as it suggests].

ii) Add Reference to the application

then finally it will execute the app.config settings to the current application.

2. Add the following settings in the app.config file:-

We can load the assemblies with the different versions in to the project by the following syntax. We can learn more details in the below msdn link.

The following syntax should be added in the app.config file.

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="A.wpf.dll" publicKeyToken="3d67ed1f87d44c89" />
        <codeBase version="3.0" href="...\A.wpf.dll"/>
        <codeBase version="5.0" href="...\A.wpf.dll"/>
        <assemblyIdentity name="B.wpf.dll" publicKeyToken="632609b4d040f6b4" />
        <codeBase version="3.0" href="...\B.wpf.dll"/>
        <codeBase version="5.0" href="...\B.wpf.dll"/>

By the above syntax i just want to add A.wpf.dll and B.wpf.dll in the respective project. Public key token needs to be added for the corresponding assembly.
We can generate the public key token by executing the following commands in the visual studio command prompt.
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin   sn.exe -T  [ assembly name ].
Wow...Finally my try gave success and everything works fine. So everyone can load the assemblies with the different versions now...

1 comment:

  1. Very nice post. I just stumbled upon your blog and wished to say
    that I've truly enjoyed browsing your blog posts. In any case I will be subscribing on your rss feed and I hope you write once more soon!
    Also see my web site > Recover deleted emails from Outlook without exchange