如何修复“System.ServiceModel.ExceptionDetail”未标记为可序列化

本文关键字:记为 序列化 ExceptionDetail 何修复 System ServiceModel | 更新日期: 2023-09-27 18:35:15

我有一个由ASP .Net网站调用的WCF Web服务。在我的 Web 方法中,我故意不捕获异常,因为我希望它们由客户端网站处理。Web 服务纯粹是一个内部应用程序,我希望它的行为类似于期望调用者处理异常的标准库。我在服务 web.config 中包含 ExceptionDetailInFaults="true"。当我的服务引发异常时,我收到此错误:

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)

如何解决此问题,以便我的客户端能够捕获服务引发的异常?

如何修复“System.ServiceModel.ExceptionDetail”未标记为可序列化

我找到了问题的原因 - 异常在我的 Web 服务中的工作线程(System.Threading.ThreadPool.QueueUserWorkItem)中抛出,所以当然没有被处理。通过添加异常处理程序并将日志记录到工作线程进行修复。