首页 > 解决方案 > 即使禁用了 DebugMode 和 WriteErrorsToResponse,响应也包括堆栈跟踪

问题描述

我在最新版本的 ServiceStack (5.6.0) 上运行自托管 API。

我正在努力在请求处理管道的早期处理异常。更具体地说,当请求包含|字符时。API 将始终使用 stackTrace 返回完整的错误响应。即使在带有DebugModeWriteErrorsToResponse禁用的发布版本中。

通常我没有问题,因为大多数问题都是在带有自定义错误响应 DTO 的自定义 ServiceRunner 中处理的。但是这个错误发生在调用 ServiceRunner 之前。

如何禁用 stackTrace?是否可以用自定义响应替换错误响应?

这是我在点击时会收到的响应对象,例如http://localhost:80/*|?

{
    "responseStatus": {
        "errorCode": "ArgumentException",
        "message": "Illegal characters in path.",
        "stackTrace": "   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)\r\n   at System.IO.Path.InternalCombine(String path1, String path2)\r\n   at System.IO.FileSystemEnumerableIterator`1.GetFullSearchString(String fullPath, String searchPattern)\r\n   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)\r\n   at System.IO.DirectoryInfo.InternalGetFiles(String searchPattern, SearchOption searchOption)\r\n   at ServiceStack.VirtualPath.FileSystemVirtualDirectory.GetFileFromBackingDirectoryOrDefault(String fName)\r\n   at ServiceStack.VirtualPath.AbstractVirtualPathProviderBase.GetFile(String virtualPath)\r\n   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()\r\n   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)\r\n   at ServiceStack.Host.HttpListener.ListenerRequest.GetFile()\r\n   at ServiceStack.Host.HttpListener.ListenerRequest.get_IsFile()\r\n   at ServiceStack.HttpHandlerFactory.GetHandlerForPathInfo(IHttpRequest httpReq, String filePath)\r\n   at ServiceStack.HttpHandlerFactory.GetHandler(IHttpRequest httpReq)\r\n   at ServiceStack.AppHostHttpListenerBase.<ProcessRequestAsync>d__8.MoveNext()",
        "errors": null,
        "meta": null
    }
}

标签: servicestack

解决方案


现在应该从这个提交中解决这个问题,它不再显示 StackTraces of HTTP Listener Exceptions when Config.DebugMode = false.

此更改从 v5.6.1+ 开始可用,现在可在 MyGet 上使用


推荐阅读