Search code examples
c#.netmef

Dll Version Control in MEF


I have an application that uses MEF to load plugins and it works just fine. All plugins reference the core library and exports the right part and the application loads the parts without any issue at all.

The issue that comes up has to do with the individual dependencies of the plugin libraries. An example is Ionic.Zip and Newtonsoft.Json. Since Nugget is used when developing these external parts, different plugins tend to reference different version of these libraries. The application may even load its own (in the case of Newtonsoft.Json).

What I've realized it that the plugin libraries are looking for a specific version of these libraries and hence throw errors when certain tasks are run.

Is there a way to indicate during development that a plugin should not be bound to any particular version of the library so that it will just make use of whatever is loaded?

That way it does not matter which version of Newtonsoft.Json or Ionic.Zip a plugin is linked to, it will just use the version that has been loaded by the main application.


Solution

  • I believe you are looking for Assembly Binding Redirect.

    You can create an assembly binding redirect for a given assembly by adding an entry in the app.config of your application. Following example will redirect all calls made to any version of myAssembly to v3.0.0.0 of the assembly

     <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="myAssembly"
              publicKeyToken="32ab4ba45e0a69a1"
              culture="en-us" />
            <!-- Assembly versions can be redirected in app, 
              publisher policy, or machine configuration files. -->
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    

    You can find more information at this link Redirecting Assembly Versions