How to use binding redirects when dynamically loading assembly with MEF

189 views Asked by At

I have a .NET 4.8 library that is loaded as a plugin via MEF. That library has automatically generated binding redirects for some of its dependencies:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
      </dependentAssembly>
    </assemblyBinding>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
      </dependentAssembly>
    </assemblyBinding>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Channels" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

My main application (.NET 4.8) loads the library assembly dynamically via MEF. When the main application loads the library (e.g. MyLibrary.dll), it appears to ignore the binding redirects that are defined in the config file (MyLibrary.dll.config) that sits alongside the library assembly, and I get a composition exception because the assembly fails to load:

Unhandled Exception: System.ComponentModel.Composition.CompositionException: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Resulting in: An exception occurred while trying to create an instance of type 'MyLibrary.MyType'.
Resulting in: Cannot activate part 'MyLibrary.MyType'.
Element: MyLibrary.MyType -->  MyLibrary.MyType -->  AssemblyCatalog (Assembly="MyLibrary, Version=3.9.0.0, Culture=neutral, PublicKeyToken=null")
Resulting in: Cannot get export 'MyLibrary.MyType (ContractName="LibraryBaseType")' from part 'MyLibrary.MyType'.
Element: MyLibrary.MyType (ContractName="LibraryBaseType") -->  MyLibrary.MyType -->  AssemblyCatalog (Assembly="MyLibrary, Version=3.9.0.0, Culture=neutral, PublicKeyToken=null")
   at System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition)
   at System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(CatalogPart part, ExportDefinition export, Boolean isSharedPart)
   at System.ComponentModel.Composition.Primitives.Export.get_Value()
   at System.ComponentModel.Composition.ExportServices.GetCastedExportedValue[T](Export export)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at MainApplication.Start(String[] args) in ...\MainApplication.cs:line 65
   at MainApplication.Main(String[] args) in ...\MainApplication.cs:line 31

Is there any way for my application to respect the binding redirects for a dynamically loaded library, if the binding redirects are defined alongside the library assembly? Or am I stuck copy-pasting all of the binding redirects into the app.config of the main application?

0

There are 0 answers