我正在尝试运行ASP.NET Core服务器。我的服务器请求返回200 OK,但是没有内容返回,从我的判断中,甚至没有实例化控制器。
Azure流日志的最后输入如下:
应用程序:2018-07-20 13:42:13.294 +00:00 [Debug] Microsoft.AspNetCore.Routing.Tree.TreeRouter:请求已成功匹配名称为'(null)'和模板'api / {的路由文化} /收到的通知/ {upToUtc} / {take}”。 应用程序:2018-07-20 13:42:13.670 +00:00 [Debug] Microsoft.AspNetCore.Server.Kestrel:连接ID“ 0HLFE8PANI4TL”已完成保持活动响应。 应用程序:2018-07-20 13:42:13.670 +00:00 [信息] Microsoft.AspNetCore.Hosting.Internal.WebHost:请求在413.5636ms中完成200
因此路由被匹配并且请求完成,但是我尝试将断点附加到控制器(远程调试器已附加,但是即使在我的中间件中也没有命中断点),并从控制器记录信息,如下所示:
[<Authorize>]
[<Route("api/{culture}/[controller]")>]
type NotificationsReceivedController(notifications: IReceivedNotifications, logger: ILog) =
inherit Controller()
do logger.Information "NotificationsReceivedController" "Created controller" None
[<HttpGet("{upToUtc}/{take}")>]
[<ProducesResponseType(200, Type = typeof<PageOfNotifications>)>]
member this.Get(upToUtc:int64, take:int) =
async {
let upToUtc = new DateTime(upToUtc, DateTimeKind.Utc)
logger.Information "NotificationsReceivedController" "GetReceivedNotifications endpoint" None
let! notifications = notifications.GetAsync this.HttpContext upToUtc take
return this.Ok(notifications) :> IActionResult
} |> Async.StartAsTask
我不了解误报。如果我的管道出现问题,为什么没有在日志中记录呢?如果没有任何问题,为什么路由会匹配,但控制器没有实例化?
答案 0 :(得分:1)
我设法使用异常过滤器解决了这个问题
type ApiError = {
ErrorType: Type
Message: string
StackTrace: string
}
type ApiExceptionAttribute(logger: ILog) =
inherit ExceptionFilterAttribute()
static let [<Literal>] Tag = "ApiExceptionAttribute"
override __.OnException(context) =
base.OnException context
context.HttpContext.Response.StatusCode <-
match context.Exception with
| As (_: UnauthorizedAccessException) -> HttpStatusCode.Unauthorized |> int
| _ -> HttpStatusCode.InternalServerError |> int
let ex = context.Exception.GetBaseException()
let apiError = {
ErrorType = ex.GetType()
Message = ex.Message
StackTrace = ex.StackTrace
}
logger.Error Tag (apiError.ToString()) (Some ex)
context.Result <- JsonResult apiError
然后,按照标准做法,我将此过滤器设置为Startup.fs
文件中的全局过滤器:
在我的Startup.fs文件中:
services.AddMvc(fun config ->
config.RespectBrowserAcceptHeader <- true
config.Filters.Add<ApiExceptionAttribute>() |> ignore
) |> ignore
这解决了我的误报,并允许我跟踪并修复代码中的错误。