question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong interpretation of public key token of core library

See original GitHub issue

Initial code used PublicKeyToken to determine if a referenced assembly is actually the core library (e.g. “mscorlib”). This wrong approach is made its way to till to date. PublicKeyToken is actually hash of public key and it’s what used during signing. So, PublicKeyToken is actually same for assemblies which Microsoft used same private key.

We should use fully qualified assembly name instead of PublicKeyToken. But, there is another problem that should be fixed as well. For example, System.Random resides in System.Runtime.Extensions assembly. If we try to get the assembly with:

var asm = typeof(System.Random).Assembly.GetName();
Console.WriteLine(asm.FullName);

// On .NET Core, prints:
// System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e

So, mapping System.Private.CoreLib to System.Runtime will lead a crash. Because, System.Runtime does not have any reference for System.Random.

So, correct core library mapping is actually somewhat more complex. One of the possible way is to build a type dictionary for known assemblies like:

var knownAssemblyNames = new []{ "System.Runtime", "System.Runtime.Extensions", /* ... */ };

foreach (var name in knownAssemblyNames) {
  var asm = Assembly.Load(name);
  var types = asm.GetTypes();
  AddKnownTypeReference(name, types);
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
OlegRacommented, May 11, 2022

@vermorel This one can be closed - the related PR #161 has already merged.

0reactions
lbmaiancommented, Jul 23, 2020

Hi, PR #61 has since been merged last year, and I’m now getting an issue where System.Private.CoreLib isn’t being mapped to System.Runtime in .NET Core 3.0 and 3.1 (haven’t tested earlier versions) builds and runtimes, in both a console app and in an NUnit test.

var assemblyName = new AssemblyName("TestAssemblyA");
var assemblyFileName = assemblyName.Name + ".dll";
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("module");
var typeBuilder = moduleBuilder.DefineType("ClassA", TypeAttributes.Public);
var fieldBuilder = typeBuilder.DefineField("field", typeof(string), FieldAttributes.Public);
var ctorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { typeof(string) });
ctorBuilder.DefineParameter(1, ParameterAttributes.None, "field");
var il = ctorBuilder.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Stfld, fieldBuilder);
il.Emit(OpCodes.Ret);
_ = typeBuilder.CreateType();
new Lokad.ILPack.AssemblyGenerator().GenerateAssembly(assemblyBuilder, assemblyFileName);

produces the following assembly: TestAssemblyA.zip

Note that this assembly has a reference to System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e rather than System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

Read more comments on GitHub >

github_iconTop Results From Across the Web

Binding redirect fails - different publicKeyToken
1 Answer. You can't redirect an assembly if the public key is different I am afraid, you will need to recompile against the...
Read more >
how to fix : Could not load file or assembly 'Azure.Core'
I have a .net framework 4.2 Test Project, which calls methods in .net 4.2 a class library called Interface. Interface references another ...
Read more >
Could not load file or assembly Microsoft.Extensions. ...
I have a Visual Studio 2019 extension that references Microsoft.Extensions.Configuration v3.1.1. This assembly gets loaded by a code ...
Read more >
The located assembly's manifest definition does not match ...
I have a WCF service which is referencing a COM dll. I keep getting the error as follows. Could not load file or...
Read more >
Binding Redirects
The fix if you're using .NET Framework · name : The name of the strongly named DLL · publicKeyToken : The public key...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found