The Unusual Ref/Def Mismatch FileLoadException

One of my colleagues has experienced a very strange Ref/Def Mismatch FileLoadException (The located assembly's manifest definition does not match the assembly reference). After a long investigation, we finally track down the cause.

To explain the problem, we have to understand the sequence of assembly loading and the interaction between fusion and CLR.

TheĀ  key is after LoadLibrary , loader does one more check to make sure the file has not been changed between the original file mapping and LoadLibrary.

In my colleague's case, the assembly is in GAC. During the short period in between, someone did an in-place update of the assembly (When the assembly is mapped, you cannot delete it, but you can move it to a different location. This is how fusion does in-place update of assembly in GAC). Very unlucky indeed.

Since the file mapping is cached per-process, it is necessary to restart the process to load the assembly successfully again.

The bottom line is, CLR assembly loading does not work well against in-place update. Please avoid it if possible.