13

I'm trying to change assembly binding (from one version to another) dynamically.

I've tried this code but it doesn't work:

      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      ConfigurationSection assemblyBindingSection = config.Sections["assemblyBinding"];

      assemblyBindingSection.SectionInformation.ConfigSource = "bindingConf.xml";
      config.Save(ConfigurationSaveMode.Modified);

      ConfigurationManager.RefreshSection("assemblyBinding");

with bindingConf.xml containing the assemblyBinding section configuration.

So can a change this section at runtime? how to do it? What alternatives do I have?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117

3 Answers3

22

The best way I've found to dynamically bind to a different version of an assembly is to hook the AppDomain.AssemblyResolve event. This event is fired whenever the runtime is unable to locate the exact assembly that the application was linked against, and it allows you to provide another assembly, that you load yourself, in its place (as long as it is compatible).

For example, you can put in a static constructor on your application's main class that hooks the event like this:

using System.Reflection;

static Program()
{
    AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs e)
    {
        AssemblyName requestedName = new AssemblyName(e.Name);

        if (requestedName.Name == "AssemblyNameToRedirect")
        {
            // Put code here to load whatever version of the assembly you actually have

            return Assembly.LoadFrom("RedirectedAssembly.DLL");
        }
        else
        {
            return null;
        }
    };
}

This method avoids the need to deal with the assembly bindings in configuration files and is a bit more flexible in terms of what you can do with it.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Eric Rosenberger
  • 8,987
  • 1
  • 23
  • 24
  • 1
    Great solution! Unfortunately in my case, I need the binding even if the default assembly is resolved. So the event will never be fired. – Julien Hoarau Nov 28 '08 at 15:33
  • Maybe this is a long shot, but can you link against a version that you know will not exist? (i.e., build a special one with a fake version number) – Eric Rosenberger Nov 28 '08 at 15:41
  • It doesn't work. That goes on an infinite loop ending by a stack overflow ^^ – Julien Hoarau Dec 01 '08 at 08:24
  • Oops -- had a typo in my code... should be "LoadFrom" instead of "LoadFile". Fixed the code and tested it and it definitely works for me now, if you're still interested... – Eric Rosenberger Dec 02 '08 at 01:14
  • This could be really useful for resolving assemblies required by PowerShell modules, where using .config is not practical. When I tried it, I saw several event for assemblies that were part of PowerShell. Am I safe to ignore these and just process the assembly in question? – Simon Dec 01 '13 at 13:25
  • If an event occurs for an assembly that you can't resolve yourself, you should just return null. There's not anything further you can do. Note that beginning with .NET 4 this event occurs for resource assemblies that may or may not be required (so the fact that they don't resolve isn't necessarily a problem), so that could be what you're seeing. – Eric Rosenberger Dec 02 '13 at 16:53
  • Worked a charm, believe it or not – magnusarinell Apr 06 '16 at 13:38
  • OMG this is awesome! No more dealing with bugging assembly bindings with NuGet's new PackageReference model. I've modified this a bit. I'll post the code as an answer. – Josh Mouch Mar 19 '19 at 16:46
4

I love Eric's answer. It's a lifesaver when trying to use the new buggy NuGet PackageReference model with a Web app. The problem is that you can have msbuild automatically generate the bindings, however, they generate the bindings to Assembly.dll.config, and not to web.config. So this workaround is great.

I've modified Eric's code a bit to make it more generic and work with an ASP.Net Core app:

AppDomain.CurrentDomain.AssemblyResolve += delegate (object sender2, ResolveEventArgs e2)
            {
                var requestedNameAssembly = new AssemblyName(e2.Name);
                var requestedName = requestedNameAssembly.Name;
                if (requestedName.EndsWith(".resources")) return null;
                var binFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/bin");
                var fullPath = Path.Combine(binFolder, requestedName) + ".dll";
                if (File.Exists(fullPath))
                {
                    return Assembly.LoadFrom(fullPath);
                }

                return null;
            };
Josh Mouch
  • 3,480
  • 1
  • 37
  • 34
3

RuntimeSection of the config file update at runtime using this code:

private void ModifyRuntimeAppConfig()
{
  XmlDocument modifiedRuntimeSection = GetResource("Framework35Rebinding");

  if(modifiedRuntimeSection != null)
  {
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationSection assemblyBindingSection = config.Sections["runtime"];

    assemblyBindingSection.SectionInformation.SetRawXml(modifiedRuntimeSection.InnerXml);
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("runtime");
  }
}

with Framework35Rebinding containing:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
      <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/>
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.CompactFramework.Build.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
      <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="9.0.0.0"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>

and an app.config containing (before the execution of the program):

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
  <runtime>
  </runtime>
</configuration>

Nevertheless it doesn't work for that I wanna do because assemblyBinding is only read at startup of the application whereas the RefreshSection("runtime")

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117