Skip to content

Instantly share code, notes, and snippets.

@natemcmaster
Last active July 8, 2016 15:32
Show Gist options
  • Save natemcmaster/58eed22234556508ae888d9b88740263 to your computer and use it in GitHub Desktop.
Save natemcmaster/58eed22234556508ae888d9b88740263 to your computer and use it in GitHub Desktop.
ALC versions

Consider this following example:

An implementation of ALC is setup to load CommonAssembly, Version=2.0.0.0, InformationalVersion=2.0.0-beta.

The host in an app that this on the TPA: CommonAssembly, Version=2.0.0.0, InformationalVersion = 2.0.0-alpha.

// in a test app that depends on CommonAssembly "2.0.0-alpha"
public class HostProgram
{
   public static void Main()
   {
       Console.WriteLine(GetCommonAssemblyInfoVersion()); // writes "2.0.0-alpha"
   }
}

Finally, the ALC will also return an assembly "BananaApp" which contains "TestClass" that will return the version of CommonAssembly.

// assembly BananaApp
// BananaApp depends on CommonAssembly
public class TestClass
{
    public TestClass()
    {
      Console.WriteLine(GetCommonAssemblyInfoVersion()); // writes the version of CommonAssembly it is using
   }
}

If the host activates BananaApp.TestClass from ALC before ALC has loaded a version of CommonAssembly, TestClass will get the host version, 2.0.0-alpha.

public class HostProgram
{
   public static void Main()
   {
       Console.WriteLine(GetCommonAssemblyInfoVersion()); // writes "2.0.0-alpha"
       AssemblyLoadContext loadContext = new MyAssemblyLoadContext();
       Assembly bananaApp = loadContext.LoadFromAssemblyName(new AssemblyName { Name = "BananaApp" });
       
       
       Activator.CreateInstance(bananaApp.GetType("TestClass")); // writes "2.0.0-alpha" but expected "2.0.0-beta"
   }
}

But if the host explicitly loads CommonAssembly in ALC first, TestClass will get 2.0.0-beta.

public class HostProgram
{
   public static void Main()
   {
       Console.WriteLine(GetCommonAssemblyInfoVersion()); // writes "2.0.0-alpha"
       AssemblyLoadContext loadContext = new MyAssemblyLoadContext();

       // the difference
      loadContext.LoadFromAssemblyName(new AssemblyName { Name = "CommonAssembly" });

       Assembly bananaApp = loadContext.LoadFromAssemblyName(new AssemblyName { Name = "BananaApp" });
       Activator.CreateInstance(bananaApp.GetType("TestClass")); // writes "2.0.0-beta", not "alpha"
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment