我想在ServiceStack中注册一个自定义异常处理程序。https://github.com/ServiceStack/ServiceStack/wiki/Error-Handling上的wiki说:
this.ServiceExceptionHandler = (request, exception) => {
//log your exceptions here
...
//call default exception handler or prepare your own custom response
return DtoUtils.HandleException(this, request, exception);
};
但是在我的异常处理程序中,request
参数不是Request对象,而是抛出异常的Service
(至少我可以从调试器中看出)。将request
转换为IHttpRequest
失败,并出现InvalidCast异常。由于Service
的Request
/Response
成员受到保护,因此我无法从异常处理程序中访问请求。
如何访问请求?特别是,我需要访问Content-Type
和在请求中作为标头发送的可接受语言列表。
从源代码来看,看起来request
应该是请求dto,但如果您需要更多,您可以创建自己的ServiceRunner<T>
,然后您也将获得IRequestContext
,它具有ContentType
属性。
https://github.com/ServiceStack/ServiceStack/wiki/Error-Handling fine-grain-error-handling-using-the-new-apis-servicerunner
编辑,更多信息:
当你覆盖HandleException
时,你可以返回一个HttpError
,这将允许你设置标题和ResponseStatus
等。