Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magick.NET 9.0.0 - The type initializer for 'NativeResourceLimits' threw an exception. #1121

Closed
MattiaCamponesco opened this issue Jan 25, 2022 · 13 comments
Milestone

Comments

@MattiaCamponesco
Copy link

Description

Dear all,
I was testing the new release of Magick.Net and I found out an exception limitating the memory of the process.

Error:
The type initializer for 'NativeResourceLimits' threw an exception.

Stack Trace:
at ImageMagick.ResourceLimits.NativeResourceLimits.LimitMemory(Double percentage) at ImageMagick.ResourceLimits.LimitMemory(Percentage percentage) in /_/src/Magick.NET/ResourceLimits.cs:line 95 at Libraries.Managers.Graphics.ImageManager..ctor(Process process) at Libraries.Managers.Graphics.GraphicsManager.Elabora_SincronizzazioneGrafiche_GestisciGrafica(DataTosGraficheServices dataTosGraficheServices) at Libraries.Managers.Graphics.GraphicsManager.Elabora_SincronizzazioneGrafiche()

Inner Exception:
System.DllNotFoundException: Unable to load DLL 'Magick.Native-Q8-x64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) at ImageMagick.Environment.NativeMethods.X64.Environment_Initialize() at ImageMagick.Environment.Initialize() in /_/src/Magick.NET/Helpers/Environment.cs:line 22 at ImageMagick.ResourceLimits.NativeResourceLimits..cctor() in /_/src/Magick.NET/Native/ResourceLimits.cs:line 137

Steps to Reproduce

Call the method ResourceLimits.LimitMemory(percentage)

image

System Configuration

  • Magick.NET version: Magick.NET.Core Magick.NET-Q8-AnyCPU
  • Environment (Operating system, version and so on): Windows 10.0.17763
  • Additional information: .NET Framework 4.7.2
@dlemstra
Copy link
Owner

It looks like your bin folder does not contain the native library. Are you doing anything special to deploy the application?

@davidei1955
Copy link

I'm having a problem very similar to this. Upgrading a Winforms .NET Framweork 4.8 app from Magick.NET-Q8-AnyCPU v8.6.1 to v9.0.0 gives me:

System.DllNotFoundException: Unable to load DLL 'Magick.Native-Q8-x64.dll': The specified module could not be found.

You're correct, the bin folder does not contain the native dll, and the problem goes away when the it is manually copied from 'packages\Magick.NET-Q8-AnyCPU.9.0.0\runtimes\win-x64\native' to the bin folder.

The odd thing is, the native dll is not in my bin folder after reverting to v8.6.1, and it works OK. Removing it from 'packages\Magick.NET-Q8-AnyCPU.9.0.0\runtimes\win-x64\native' and rebuilding doesn't recreate the problem in v8.6.1, either. I see that the size of Magick.NET-Q8-AnyCPU.dll is more than 17MB in v8.6.1, but less than 700KB in v9.0.0, so I suspect that the native dll used to be embedded in the managed dll, but no longer is?

@dlemstra
Copy link
Owner

dlemstra commented Jan 28, 2022

That is correct. In the previous version I had to embed the dll inside the library to make this work. But it looks like the native library is not automatically copied to the bin folder with .NET Standard 2.0 when .NET framework is being targeted. I will need to figure out why that is happening first before I can check if I can add something to make this happen. What kind of .csproj are you both using (in other words, the old or the new format)? Do you have a packages.config file in the project?

@davidei1955
Copy link

I have packages.config in every project. I’ve attached one. I think my .csproj files are are old style, but I’m not sure. They’re attached, too. Let me know if you need anything else.
MagickIssues.zip
.

@MattiaCamponesco
Copy link
Author

For what concern me, the situation is similar to @davidei1955 one.
The only one difference is that I have only onw Library" project in which Magick.NET libraries are installed.
So I think you can follow one of use to understand the issue.

@hakakou
Copy link

hakakou commented Feb 1, 2022

Same issue with my projects (Test and Web) that target 4.7.2 framework. After upgrading to v9, Magick.Native-Q16-x64.dll is not found. However I temporary fixed the problem by copying Magick.Native-Q16-x64.dll to a folder in the system's environment paths.

@dlemstra dlemstra added this to the 9.1.0 milestone Feb 6, 2022
@dlemstra
Copy link
Owner

dlemstra commented Feb 6, 2022

A patch for this has been added to the 9.1.0 release. Can you give it another try with the new release that has just been published?

@MattiaCamponesco
Copy link
Author

MattiaCamponesco commented Feb 7, 2022

Personally, I tried but I get the same error as before.
I can't see the Magick.Native-Q8-x86.dll anywhere.

image

@dlemstra
Copy link
Owner

dlemstra commented Feb 7, 2022

Is it possible to create a small project that demonstrates this issue and share it on GitHub?

@MattiaCamponesco
Copy link
Author

Of course, I have uploaded a sample repository with a C# console application demo in which you can reproduce the issue we are facing (https://github.com/MattiaCamponesco/Magick.NET_9.1.0_demo/invitations)

@dlemstra
Copy link
Owner

dlemstra commented Feb 7, 2022

Thanks for the repository. I think I found a proper fix for it now. Can you try it again with release 9.1.1?

@viwodev
Copy link

viwodev commented Feb 8, 2022

I had the same problem too, updating from v8 to v9, getting the exception with same native dll not found.

I've just updated to v9.1.1, it's ok now, works.

@MattiaCamponesco
Copy link
Author

Yes, I confirm, v9.1.1 works. Thanks!

@dlemstra dlemstra closed this as completed Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants