19

I need to take a built version of an C# application and change one of the reference dll's. What is the best way to do this, I have specific version turned off on the reference dll but as soon as I test replacing the dll with a newer version, I get the "Could not load file or assembly XXXXX, Version=XXXXX. Is there a way to stop the loader from caring about the version of the dll so the dll will just attempt to load?

Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
Jeff Lundstrom
  • 1,043
  • 1
  • 8
  • 22

1 Answers1

20

Yes, you can do this - see the MSDN article Redirecting Assembly Versions.

You should read the whole document, but it essentially involves either the assembly's publisher creating a 'publisher policy file' or the consumer adding a bindingRedirect to an app.config file, like this (copied directly from the article):

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="myAssembly"
                           publicKeyToken="32ab4ba45e0a69a1"
                           culture="en-us" />
         <bindingRedirect oldVersion="1.0.0.0"
                          newVersion="2.0.0.0"/>
       </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

A few notes:

  • If you haven't explicitly specified your culture (as many don't), it will be "neutral" rather than "en-us".

  • If you don't already know it, you can get the assembly's public key token using the strong name utility, like this: sn -t [AssemblyPath]

Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
  • That would require a recompile, wouldn't it? – Michael Todd Sep 22 '09 at 17:56
  • Or, os the configuration file checked only at runtime? – Michael Todd Sep 22 '09 at 17:59
  • 2
    Those xml files are checked at runtime. See http://msdn.microsoft.com/en-us/library/yx7xezcf(VS.71).aspx – Brian Sep 22 '09 at 18:01
  • @Michael - indeed, what Brian said. :) – Jeff Sternal Sep 22 '09 at 18:18
  • Can I use this `Publisher Policy File` to reference any version of the `Microsoft.Office.Interop.PowerPoint` assembly from my C# WinForms app? – JohnB Nov 20 '10 at 18:47
  • What if publicKeyTokens are different? I had publicKeyToken="null" in old assembly. And this solution does not help me. – Anton Nov 13 '12 at 11:26
  • Update link [Redirecting Assembly Versions](https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions) and [How the Runtime Locates Assemblies](https://learn.microsoft.com/en-us/dotnet/framework/deployment/how-the-runtime-locates-assemblies) – SubmarineX Nov 05 '19 at 08:27