我正在尝试将范围(或this
)传递给我的自定义异常并正常显示它。我的代码工作,直到我将scope属性添加到自定义异常。我看到其他人遇到过此问题(link1,link2,link3),但没有解决方法。任何解决方案或解决方法将不胜感激。谢谢。
我的自定义例外:
public class CustomException : Exception
{
private readonly object _scope;
public CustomException(string message, object scope = null) : base(message)
{
_scope = scope;
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Scope", _scope);
base.GetObjectData(info, context);
}
}
我如何使用例外:
throw new InvalidCustomCapsBadSumException("Error message is here!, this);
Startup.cs:
services.AddMvc(config =>
{
config.Filters.Add(typeof(ExceptionFilterAttribute));
}).AddJsonOptions(x =>
{
// ignore circular loops while deserializing
x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
ExceptionFilterAttribute.cs:
public class ExceptionFilterAttribute : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
context.Result = new BadRequestObjectResult(new
{
Exception = context.Exception
});
}
}
我收到错误:
An unhandled exception was thrown by the application.
System.Runtime.Serialization.SerializationException: Serializing delegates is not supported on this platform.
at System.MulticastDelegate.GetObjectData(SerializationInfo info, StreamingContext context)