I have a WCF web service called by an ASP .Net web site. In my web method I deliberately don’t catch exceptions, as I want them to be handled by the client web site. The web service is purely an internal application, and I want it to behave like a standard library which expects the caller to handle exceptions. I have includeExceptionDetailInFaults=”true” in the service web.config. When my service throws an exception I get this error:
Log Name: Application
Source: ASP.NET 2.0.50727.0
Date: 24/05/2012 09:13:52
Event ID: 1334
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Description:
An unhandled exception occurred and the process was terminated.
Application ID: DefaultDomain
Process ID: 2628
Exception: System.Runtime.Serialization.SerializationException
Message: Type 'System.ServiceModel.ExceptionDetail' in Assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
StackTrace: at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeObject(Object obj, MemoryStream stm)
at System.AppDomain.Serialize(Object o)
at System.AppDomain.MarshalObject(Object o)
How to fix this so my client is able to catch exceptions thrown by the service?
I found the cause of the problem – the exception was being thrown in a worker thread (System.Threading.ThreadPool.QueueUserWorkItem) within my web service, so of course was not being handled. Fixed by adding an exception handler and logging to the worker thread.