Load C# from Python: Unable to Load the .NET dependencies while accessing the methods

Viewed 22

Environment

Pythonnet version: 3.0.0a2 PRE-RELEASE Python version: 3.10.5 Operating System: Windows 10 .NET Runtime: .Net core 6.0 and 5.0

Details

Have created a simple program to

  1. Add 2 numbers
  2. Read XML from File
  3. Convert Base64 Encode Used PythonNet CLR to import the dll and access all the above methods

On .NET core 6.0:

  • Add 2 numbers worked like charm
  • Read XML and Covert Base64 threw error
  • System.TypeLoadException: Could not load type 'System.Text.Encoding' from assembly 'System.Text.Encoding, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. at DllExport.XMLReader(String filePath)

On .NET core 5.0 :

Add 2 numbers worked Read XML worked Convert Base64 did not work and threw error System.TypeLoadException: Could not load type 'System.Convert' from assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. at ReusableLibariesConsole.Program.Base64_Encode(Byte[] data)

We have set the .NET version to 2.0 then all 3 errors disappeared however further methods such as Encryption did not work again System.TypeLoadException: Could not load type 'System.Security.Cryptography.PasswordDeriveBytes' from assembly 'System.Security.Cryptography.Csp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. at DllExport.EncryptionManagerClass.Encrypt(String inputData, String password, Int32 bits)

We tried lot of fixes such as setting CPU to x64, changing target framework and nothing worked. Please let us know if you need further information

1 Answers

I happened to have been battling with a similar issue about a week ago

with the error: System.TypeLoadException: Could not load type 'System.Data.Dataset' form assembly 'System.Data.Common', Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

To resolved this I downgraded the .NET version to 4.8

Related