Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Assembly not found by my DLL. Wrong strong name identity of assembly #7

Closed
tarasov65536 opened this issue Oct 16, 2019 · 3 comments · Fixed by #21
Closed

Assembly not found by my DLL. Wrong strong name identity of assembly #7

tarasov65536 opened this issue Oct 16, 2019 · 3 comments · Fixed by #21
Assignees
Labels
bug:builing Building in Unity fails due to bug bug Something isn't working

Comments

@tarasov65536
Copy link

tarasov65536 commented Oct 16, 2019

Expected behavior

I have dotnet core library that uses Newtonsoft.Json 12.0.1 as dependency. I want to use that library in my unity project. I installed Newtonsoft.Json-for-Unity with package manager and added dll with my library to Assets/Plugins folder of my unity project.

Actual behavior

When I try to use any classes from my library it says:
error CS0012: The type 'JsonSerializer' is defined in an assembly that is not referenced. You must add a reference to assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.

As I understand AssemblyVersion for original Newtonsoft.Json is always set to 12.0.0.0, but AssemblyVersion of Newtonsoft.Json-for-Unity is set to 12.0.1.0

Steps to reproduce

  • Make library with dotnet core with class that want some staff from Newtonsoft (ex: JsonSerializer).
  • Compile library with dotnet core.
  • Make new unity project.
  • Install Newtonsoft.Json-for-Unity with package manager.
  • Copy library.dll to unity project.
  • Try to instantiate new JsonSerializer and pass it to some function of your library.
@applejag
Copy link
Owner

applejag commented Oct 18, 2019

Hi @tarasov65536. This is an issue I hoped not to be needed to handle yet. But here goes.

The versioning is irrelevant in this case. It would if your dll requires newtonsoft.json 12.0.0 and I supply 12.0.1. But that's not what's causing the issue.

Problem is that the official Newtonsoft.Json you use from NuGet is compiled as a strongly named assembly with a key pair with public key token 30ad4fe6b2a6aeed (as you can spot in the error message) and my plugin is compiled without the key (because James, initial creator of the NuGet package, holds it private)


I've yet not found a good way to reference an assembly without the strong name. Maybe possible.

In a different project of mine I did a workaround where I referenced the /Src/Newtonsoft.Json/Newtonsoft.Json.csproj project from this repo instead of the one from NuGet. That way when it compiled it did so without the strong name reference.

Will revisit if I find better solution. Maybe it's staring me in the eye.

@applejag applejag added bug:builing Building in Unity fails due to bug bug Something isn't working labels Oct 18, 2019
@applejag applejag changed the title Wrong AssemblyVersion Assembly not found by my DLL. Wrong strong name identity of assembly Oct 21, 2019
@applejag
Copy link
Owner

Solution found

Came over this blog post, actually written by a teacher of mine, Fredrik Haglund, about setting the key of an assembly without access to the private key: https://blog.fredrikhaglund.se/blog/2009/02/06/how-to-patchfixhack-someone-elses-assembly/

With some testing where I applied the technique on the Newtonsoft.Json-for-Unity package build and ran the test suite inside Unity, it seems to work marvels! Unity doesn't seem to verify the assembly strong names at all. Not by the Mono compiler and not by the IL2CPP compiler.

About IL2CPP, I've been able to dig up something from my emails that is of value; a response from an issue report to Unity I did the start of this year regarding signed assemblies:

From: Unity QA [email protected]
Date: Thu, 28 Mar 2019 15:04:41 +01:00

"After some more investigation, we've found that supporting signed assembly loading for IL2CPP is not something Unity will do currently. We would need to include a good bit of code in the class libraries for cryptographic algorithms, and we don't believe the cost of the code size impact is worth the benefit of supporting signed assemblies now. We may consider support signed assemblies in the future, but for now we will not."

However, about Mono, I'm unsure about it's capabilities on checking the DLL's. Maybe even anti-virus might grab it on some system scan.


Have gotten it to build and run. Also tried using official Google.Apis.Core 1.42.0 inside the Unity project and it built correctly (once I had the correct versions setup) and was able to use Newtonsoft.Json via Google.Apis.Core in a test build with both Mono and IL2CPP.

Now off to deploying it. These are important changes!

@applejag applejag self-assigned this Dec 14, 2019
This was referenced Dec 30, 2019
@applejag
Copy link
Owner

This is fixed in version 12.0.102. The package will have a weak sign of the same public key used in the official Newtonsoft.Json.

This means, from 12.0.102 and forth, you target the official Newtonsoft.Json in your class libraries when building, then you use Newtonsoft.Json-for-Unity in Unity. The workflow should be seamless.

Note: Due to Unity not supporting Binding Redirects you can only use Newtonsoft.Json-for-Unity (this project) when targeting the same major as I've released. Meaning only 12.0.1, 12.0.2, 12.0.3 etc. will work, but 11.0.1, 10.0.1, 9.0.1 (and soon 13.0.1) etc does not work. For that you'll have to wait for me to release a backport to 11.0.1 and so forth.

Excuse the wait, xmas times you know :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug:builing Building in Unity fails due to bug bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants