Load .NET GAC assembly error

Hello, I have made an .NET assembly and registered it with gacutil tool on Windows 11.
I am able to load this assembly with following C# code from a .NET WPF project:

        AssemblyName aName = new AssemblyName();
        aName.Name = "ScriptExtMod.Netd";
        zLibAssembly = Assembly.Load(aName);

But when running same code in a script which been attached as a component in Unity, I get following error:

System.AppDomain.Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity) (at <605bf8b31fcb444b85176da963870aa7>:0)
(wrapper remoting-invoke-with-check) System.AppDomain.Load(System.Reflection.AssemblyName,System.Security.Policy.Evidence)
System.AppDomain.Load (System.Reflection.AssemblyName assemblyRef) (at <605bf8b31fcb444b85176da963870aa7>:0)
(wrapper remoting-invoke-with-check) System.AppDomain.Load(System.Reflection.AssemblyName)
System.Reflection.Assembly.Load (System.Reflection.AssemblyName assemblyRef) (at <605bf8b31fcb444b85176da963870aa7>:0)
LoadTest.Start () (at Assets/LoadTest.cs:34)```

What could cause the file not found error? Does unity allow load assembly in GAC(Global assembly cache)?

I don’t think unity supports anything like this.

Everything needs to be present and in the Assets/ folder of your project for Unity to consider it, AFAIK.

Besides, most recent business-land kinda C# stuff isn’t compatible with Unity because it either uses stuff Unity doesn’t implement, or it references a newer .NET or C# level.

… or isn’t compatible with Unity’s Mono-based (read: cross-platform) .NET implementation. NETD and gacutil registration of DLLs sounds an awful lot like stuff that would only work on Windows.

You really don’t need to do this anyway. Because that would require to separately install your DLL on the user’s system, which likely means creating an installer for your app/game that installs the game AND the separate DLL - the app will no longer function “as is” if it were to rely on a system-installed DLL. Of course this only matters if you want to distribute the app.

You should rather create a custom Unity-enabled DLL or simply write your code within Unity but use an Assembly Definition file to keep it in a separate assembly (DLL).

Thanks for reply.

I have copied the dll to Assets folder, and load function triggered following error:

Run the peverify utility against this for more information.```

And peverify tools return following error:
```PS C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8.1 Tools> .\PEVerify.exe D:\Workspace\app_main\zLibsUnitTest\Assets\ScriptExtMod.Netd.dll

Microsoft (R) .NET Framework PE Verifier. Version 4.0.30319.0
Copyright (c) Microsoft Corporation. All rights reserved.

[MD]: Unverifiable PE Header/native stub.
1 Error(s) Verifying D:\Workspace\app_main\zLibsUnitTest\Assets\ScriptExtMod.Netd.dll```

I know this may not related to Unity, but is there any possible solution for this?

Did you follow the manual on how to create a DLL that’s compatible with Unity?

Note that you will NOT be able to use WPF stuff in Unity DLLs.

1 Like

Thanks for reply.

I have made a managed dll written in C++/CLI, following function inspect the dll as an .net assembly

public static bool IsAssembly(string path)
{
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

// Try to read CLI metadata from the PE file.
using var peReader = new PEReader(fs);

if (!peReader.HasMetadata)
{
return false; // File does not have CLI metadata.
}

// Check that file has an assembly manifest.
MetadataReader reader = peReader.GetMetadataReader();
return reader.IsAssembly;
}

dll file dependencies:
9133294--1268143--upload_2023-7-8_21-18-46.png

but in Unity editor, my dll has been inspected as an native type dll.
9133294--1268131--upload_2023-7-8_19-25-56.jpg