Why Assembly.LoadFrom behaves different?

Viewed 79

I have this code running on .NET Framework 4.7.2:

static void Test()
{
    Console.WriteLine("--1--");
    LogAllLoadedAssemblies();

    Console.WriteLine("--2--");
    var loaded = Assembly.LoadFrom("C:/MyLib/1_1_0/MyLib.dll");
    Console.WriteLine(AssemblyToString(loaded));

    Console.WriteLine("--3--");
    LogAllLoadedAssemblies();
}

static void LogAllLoadedAssemblies()
{
    foreach(var str in AppDomain.CurrentDomain.GetAssemblies().Select(AssemblyToString))
        Console.WriteLine(str);
}

static string AssemblyToString(Assembly a)
{
    return $"{a.FullName}, loc: {a.Location}, ctxt:{a.HostContext}";
}

When method Test is running, i got 2 different results on my PC (Windows) & on TeamCity build agent (Mac OS X):

My PC (Windows):

--1--
MyLib, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null, loc: C:\MyLib\2_2_0\MyLib.dll, ctxt:0
--2--
MyLib, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null, loc: C:\MyLib\1_1_0\MyLib.dll, ctxt:0
--3--
MyLib, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null, loc: C:\MyLib\2_2_0\MyLib.dll, ctxt:0
MyLib, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null, loc: C:\MyLib\1_1_0\MyLib.dll, ctxt:0

TeamCity build agent (Mac OS X):

--1--
MyLib, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null, loc: C:\MyLib\2_2_0\MyLib.dll, ctxt:0
--2--
MyLib, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null, loc: C:\MyLib\2_2_0\MyLib.dll, ctxt:0
--3--
MyLib, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null, loc: C:\MyLib\2_2_0\MyLib.dll, ctxt:0

So, in 1st case LoadForm really loads assembly, I asked, regardless of the fact that the 2.2.0 ver. of this library has already been loaded.

In 2nd case it ignored my query & gave me the assembly already loaded. Why the behaviour is different?

What I did on the way of attempt to figure out what the cause of this difference:

I found [here]:

The LoadFrom method has the following disadvantages. Consider using Load instead.

  • If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

// what "identity" means here? UPD: comments said "identity" means an assembly name, version, culture & public-key-token

But why on my PC (1st case) this rule does not work?

I've red about some possible different contexts of loaded assemblies. But I see, that .HostContext of all assemblies I've ever seen at AppDomain.CurrentDomain.GetAssemblies() is always 0. So, no matter what "context" is stands for, looks like it's the same for all assemblies in both cases, and probably it doesn't answer my question.

0 Answers
Related