Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / WPF
Tip/Trick

Load the same assembly with different versions

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
27 Apr 2012CPOL1 min read 31.5K   3   5
Load different version of same assemblies in the wpf application

Introduction

Normally we can load only the same version of assemblies into an application. But now the requirement is, we have a WPF application and also a class library. Our class library will have version 5.0 of assemblies and my exe project will having the 3.0 version of the assemblies. We need to load these versions of assemblies into the exe project. 

Using the code 

To execute the above requirement, we should do the following steps very carefully.

  1. Add the app.config file in the application.
  2. When we create the WPF application, by default, we will not have the app.config file in the application as in the ASP.NET project. But we can achieve this by doing a couple of steps.

    1. Right click in the project properties and choose the ApplicationConfiguration file and the name should be App.config [not App1.config as it suggests].
    2. Add Reference to the application.
    3. Then finally it will execute the app.config settings to the current application.

  3. Add the following settings in the app.config file.
  4. We can load the assemblies with different versions into the project using the following syntax. We can learn more details in the below MSDN link:

    http://msdn.microsoft.com/en-us/library/15hyw9x3(v=vs.100).aspx 

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

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

By the above syntax I just want to add A.wpf.dll and B.wpf.dll in the respective project. The public key token needs to be added for the corresponding assembly. We can generate the public key token by executing the following command in the Visual Studio command prompt. 

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin   sn.exe -T  [ assembly name ].

Wow...Finally my trial was successful and everything worked fine. So now we can load assemblies with different versions... 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
David working as a WPF and Silverlight developer in Chennai. He actually doing the custom control development in the WPF/Silverlight platforms.

http://wpftution.blogspot.in/

Comments and Discussions

 
QuestionIs there a way of doing this programmatically? Pin
5Arete231-May-14 11:19
5Arete231-May-14 11:19 
Questionhow the app can know which version it should load from the href? Pin
ericpxz27-Apr-12 6:37
ericpxz27-Apr-12 6:37 
AnswerRe: how the app can know which version it should load from the href? Pin
David Bekham28-Apr-12 6:14
David Bekham28-Apr-12 6:14 
GeneralRe: how the app can know which version it should load from the href? Pin
ericpxz29-Apr-12 7:31
ericpxz29-Apr-12 7:31 
GeneralRe: how the app can know which version it should load from the href? Pin
Thamarai from Coimbatore24-Jul-13 23:31
Thamarai from Coimbatore24-Jul-13 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.