This project has moved and is read-only. For the latest updates, please go here.

Exception System.Web.HttpContextWrapper is not marked as serializable

Mar 13, 2015 at 8:12 PM
I am using Magick.NET-AnyCPU.dll in an application using IIS 7.0. Any method call on MagickNET, such as "MagickNET.UseOpenCL = false;" generates an exception, which has as its innermost InnerException the following:

System.Runtime.Serialization.SerializationException: Type 'System.Web.HttpContextWrapper' in Assembly 'System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable

The full stack trace is below.

I'd appreciate any thoughts why this exception is generated.

"System.InvalidOperationException: Failed to load embedded x64 assembly: Failed to load embedded x64 assembly: unable to initialize Magick.NET --->
System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.\n ---> System.Runtime.Serialization.SerializationException: Type 'System.Web.HttpContextWrapper' in Assembly 'System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.\r\n\r\nServer stack trace: \r\n
at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)\r\n
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)\r\n
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()\r\n
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Obje
ct obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)\r\n
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)\r\n
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)\r\n
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)\r\n
at System.Runtime.Remoting.Messaging.SmuggledMethodCallMessage..ctor(IMethodCallMessage mcm)\r\n at System.Runtime.Remoting.Channels.CrossAppDomainSink.SyncProcessMessage(IMessage reqMsg)\r\n\r\nException rethrown
at [0]: \r\n at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n at System.Runtime.Remoti
ng.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)\r\n at System.AppDomain.get_Id()\r\n
at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)\r\n
at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )\r\n
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )\r\n --- End of inner exception stack trace ---\r\n
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )\r\n
at .cctor()\r\n --- End of inner exception stack trace ---\r\n
at System.Delegate.BindToMethodInfo(Object target, IRuntimeMethodInfo method, RuntimeType methodType, DelegateBindingFlags flags)\r\n
at System.Delegate.CreateDelegateNoSecurityCheck(Type type, Object target, RuntimeMethodHandle method)\r\n
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType)\r\n
at Fasterflect.Emitter.MethodInvocationEmitter.CreateDelegate()\r\n
at Fasterflect.MethodExtensions.DelegateForCallMethod(Type type, Type[] genericTypes, String name, Flags bindingFlags, Type[] parameterTypes)\r\n
at ImageMagick.AssemblyHelper.LoadAssembly(String cacheDirectory, String tempFile)\r\n
at ImageMagick.AssemblyHelper.LoadAssembly()\r\n --- End of inner exception stack trace ---\r\n
at ImageMagick.AssemblyHelper.get_Assembly()\r\n at ImageMagick.MagickImage..ctor(Byte[] data)\r\n
at Bankrate.Web.Extensions.MagickImageExtension.CreateMagickImage(String uri)"
Mar 14, 2015 at 10:35 AM
It looks like AnyCPU library is unable to load the embedded assembly. Can you set MagickAnyCPU.CacheDirectory to a folder you can write to?
Mar 16, 2015 at 1:11 PM
Thanks for your reply.

Yes, I am able to set the MagickAnyCPU.CacheDirectory to a folder I can write to, in the statement prior to calling MagickNET.UseOpenCL = false. However, setting the cache directory seems to have no bearing on the outcome...

Thanks again for the help!
Mar 16, 2015 at 1:45 PM
More grist for the mill - it looks like the IIS application pool is trying to load the assembly in its own isolated domain, which is probably why the call DoCallBackInDefaultDomain is in the stack trace. Since the assembly is loaded in an isolated domain, the framework is trying to serialize an object, which somehow includes the HttpContextWrapper property.

I hope this helps!
Mar 16, 2015 at 1:46 PM
Edited Mar 16, 2015 at 2:34 PM
Do you see files being created in the folder you set for 'MagickAnyCPU.CacheDirectory'? And are you making sure you don't call any Magick.NET code before you set the CacheDirectory?

It might also be possible that FasterReflecter won't work in an isolated domain. I am removing the dependency on FasterReflect in the next release. But that is a lot of work so that might take a while. Can you switch to the x86 or the x64 version for now?
Mar 16, 2015 at 2:07 PM
Thanks for the immediate feedback.

I definitely see that the folder 'Magick.NET." is created within the MagickAnyCPU.CacheDirectory, as are the XML files and DLL.

Thanks for the heads-up on FasterReflector. I will try to use the x86 or x64 versions to see if the isolation problem goes away.

Thanks again for the guidance!

Mar 16, 2015 at 6:43 PM
Dirk, I've narrowed it down to ASP.NET projects with Glimpse installed. I created two ASP.NET projects, with and without Glimpse, and the error appears in the project with Glimpse, but the project without Glimpse has no issues. If I remove the Glimpse package, everything works again...

The Glimpse forum reports issues with this particular exception, for which the current release has a patch called the Async Patch:

I've tested the release with the web.config setting, and the "broken" ASP.NET project with Glimpse starts to work.

Thanks again for your help!

Mar 16, 2015 at 8:07 PM
Thanks for getting back to this topic, this info might help somebody else.