我相信无效 XML 的序列化程序引发的异常是 InvalidOperationException,内部异常类型为System.Runtime.Serialization.SerializationException
如果您检测到这一点,您可以执行所需的错误特定处理。例如:
public bool HandleError(Exception error)
{
string output = "Unknown error";
if (error.InnerException is System.Runtime.Serialization.SerializationException)
{
output = "Malformed message";
}
TraceSource traceSource = new TraceSource("YourTraceSource");
traceSource.TraceEvent(TraceEventType.Error, 0, output);
return false;
}
或者
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error.InnerException is System.Runtime.Serialization.SerializationException)
{
//set malformed message status code (400?)
}
else
{
//set other status code
}
...
}