Search code examples
c#winformsinstallationassembly-binding-redirect

Binding redirect not applying after installation


Recently I naively ran the 'Update-Package' command in the Package Manager Console thinking it would be good to have the most recent versions of things. It's caused no end to problems, and while I have handled most, the latest one has stumped me.

It has proved difficult to fix because it does not occur while developing. After installing my application, attempting a certain operation prompts this error, even on my development machine:

Error message describing failure to load version 3.0.0.0 of Microsoft.Data.SQLClient

This occurs on multiple assemblies, I've just chosen this one as an example. In the project, I have a BindingRedirect to use version 4.0.0.0 in my app.config file:

<configuration>

  [Settings]

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

      [Other redirects]

      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.SqlClient" publicKeyToken="23ec7fc2d6eaa4a5" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>

      [Other redirects]

    </assemblyBinding>
  </runtime>
</configuration>

(Notice that the PublicKeyToken matches between the error and the BindingRedirect)

In my solution, the reference for Microsoft.Data.SqlClient is version 4.0.0.0 and has 'Copy Local' set to true. My installer (using Advanced Installer) successfully places a copy of the matching .dll into the installed application's directory.

I've attempted to search for fixes, but most solutions deal with a malformed app.config file or getting a development build to finish. As far as I can tell my app.config is fine, and the problem only occurs after installation.

Is there some step I need to take to explicitly embed the binding redirects into my application? Could this be a problem with the installer failing to transfer something from app.config?

I'm continuing to look for solutions, but wanted to ask here in the meantime in case there's something obvious I'm missing. Thanks for reading.


Solution

  • I found my own solution, for future Googlers:

    Using 'Update-Package' generated a lot of BindingRedirects inside app.config. Building the project creates an ApplicationName.exe.config in the project's output directory that contains those redirects. The problem was that Advanced Installer does not automatically update the resulting ApplicationName.exe.config file. (It resolves many things on its own, this is not one of them!)

    I removed and re-added that configuration file; the new version contained the redirects my application needed and I was able to run smoothly again.