如何在ServiceStack的自定义异常处理程序中访问Request对象



我想在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异常。由于ServiceRequest/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等。

最新更新