Failed to load embedded assembly: The type initializer for 'ImageMagick.MagickNET' threw an exception.

Jan 5, 2015 at 11:22 AM
Hi,
I am facing an issue with Magick.Net x64 dll not getting loaded when accessing from IIS. Here are the details:
Machine Details: Windows 7 SP1 (x64)
  1. Installables.png - The Installables have both x86 and x64 installed as redistributable on my machine.
  2. FailToLoadAssembly.jpg - This shows the exception thrown by ImageMagick.MagickImage class constructor when attempting to load assembly.
  3. StackTrace.jpg - Attached is the stacktrace that we are seeing.
  4. TempLocationInformation.jpg - Note that Environment.Is64bitProcess is true & ImageMagick cache directory is set to "C:\Windows\Temp" as expected. Also; it has "Magick.Net-Q16-x64.dll"
All this happens only when loading the web site from IIS; however; if the site is executed from Visual Studio 2012 development environment; the cache directory location changes to
"C:\Users\<username>\AppData\Local\Temp" and the x86 dll is loaded in here which makes it work.

Kindly suggest any fix / missing section in here which is not allowing it work when loading the web site from IIS.

Attempts made:
  1. Turning the IIS application pool from Enable 32 bit = false to true does not work for us as we are expected to load the application pool as 64 bit.
  2. Attempts made to change the cache directory by setting it explicitly to appdata\local\temp throws an exception - could not find directory
Attachment(s): Details.zip having all screenshots in it.
http://1drv.ms/1tJLD53

Regards,
Mandaar Jani
Coordinator
Jan 5, 2015 at 11:50 AM
When are you setting the cache directory? You should set this before you use any of the other Magick.NET classes.
Jan 5, 2015 at 1:29 PM
Yes, that is what we tried before the statement gets executed we tried to set the cache directory. It didn't work so.

Can you please suggest anything else that we are missing?
Coordinator
Jan 5, 2015 at 2:18 PM
Can you upgrade to the latest version of Magick.NET and try again?
Jan 6, 2015 at 4:35 AM
No, we cannot upgrade because we are working on a hotfix pack for our customers. It might not be advisable to do so.

Also; just so that you know; every other machine in our network is able to load the dll and we are able to read jpeg encoded tiff images. It's just that few machines are not able to do so - the ones that do are all windows 7 x64 and the ones that do not are also windows 7 x64. All of them have VC++ redistributable for VS 2012 installed for both x64 and x86.

What are the other options ?
Coordinator
Jan 6, 2015 at 6:52 AM
Can you get more information from the InnerException of 'FailToLoadAssembly.jpg'?
Jan 6, 2015 at 10:07 AM
Here's the link for the inner exception that I uploaded to onedrive - http://1drv.ms/13VOv2r

I also observed this is similar to the thread https://magick.codeplex.com/discussions/553305 ; in which one of the thread discussion says it works with LocalSystem account but not with Networkservice / LocalService / ApplicationPoolIdentity.

We are running all our applications under ApplicationPoolIdentity ; so here's the next question how come other machines are running under the same application pool identity and still able to make it work and why this does not work on my machine?

Observations:
  1. When I turned the identity to LocalSystem account - I saw it was writing / creating a folder and a profile information under C:\Windows\System32\config\systemprofile\AppData\Local.config\ImageMagick
  2. Next, I tried UseOpenCL flag to set it to true but that threw me the same innerexception attached in the screen shot (shorten link referenced above) which means; the open cl option might not even work.
Awaiting further guidance!

Thanks!
Coordinator
Jan 6, 2015 at 10:50 AM
Edited Jan 6, 2015 at 10:51 AM
I am not sure if this is available in your version of Magick.NET but can you also do the following at the start of your application:
MagickNET.SetOpenCLCacheDirectory(@"C:\Windows\Temp");
or you could try disabling OpenCL acceleration:
MagickNET.UseOpenCL=false;
Jan 6, 2015 at 11:06 AM
Edited Jan 6, 2015 at 11:08 AM
Hi,
Thanks for the information; I tried that earlier per one of the older threads.

I tried it once again but it seems it is not able to refer the MagickNET class at all and so accessing this property itself throws the same exception.

Here's what I get - http://1drv.ms/1wRCnpY

On more observation; I saw that there is no method available called MagickNET.SetOpenCLCacheDirectory. Our current version is 6.8.9.101
Coordinator
Jan 6, 2015 at 12:30 PM
Edited Jan 6, 2015 at 12:30 PM
I checked the history and the feature to set the OpenCL cache directory was added after that release. So you need to upgrade or make sure that your application has write access in %LOCALAPPDATA%.
Jan 7, 2015 at 5:03 AM
Okay Thanks a lot for your help!

We'll try to make it in the next hotfix pack / release versions.

Appreciate your help once again.