OData 错误响应缺少异常详细信息 C#

OData Error Response missing exception detail C#

我的本地主机上有 C# WebApi2 项目 运行,如果发生 OData 错误,我会收到以下格式的错误

https://localhost:44379/api/v2/events?$filter=1

{
  "error": {
    "code": "",
    "message": "The $filter expression must evaluate to a single boolean value.",
    "innererror": {
      "message": "The $filter expression must evaluate to a single boolean value.",
      "type": "Microsoft.OData.ODataException",
      "stacktrace": "**STACKTRACE**"
    }
  }
}

然而,当我将此 api 部署到 Azure webapp 和完全相同的查询时,我得到以下信息。

https://myapi.azurewebsites.net/api/v2/events?$filter=1

{
  "error": {
    "code": "",
    "message": "An error has occurred."
  }
}

起初我认为它可能只是非调试版本的设置。我设法覆盖 ODataErrorSerializer 以添加日志记录以尝试进一步调试问题。但是,仔细观察后,似乎参数 graph(传递给此方法 https://github.com/OData/WebApi/blob/f9d10191efcb13fee7f995fa4ec2188860d8c6fd/src/Microsoft.AspNet.OData.Shared/Formatter/Serialization/ODataErrorSerializer.cs#L26 的参数不具有它通常具有的所有属性(stacktrace、innererror 等)。但这只发生在我将代码部署到 Azure?

我有一个异常记录器,它可以很好地拾取异常(在 Azure 中),但在处理 OData 错误时似乎没有相同的信息。

这可能是某种网络服务器 (IIS) 问题吗?我设法获得了 http 调用的堆栈跟踪,这里的某个地方是数据进入矩阵的地方。有没有人遇到过这样的事情?

StackTrace: ' at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo) at System.Environment.get_StackTrace() at MYAPI.EFODataErrorSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext) at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders) at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) at System.Net.Http.Formatting.MediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext) at System.Net.Http.ObjectContent.SerializeToStreamAsync(Stream stream, TransportContext context) at System.Net.Http.HttpContent.CopyToAsync(Stream stream, TransportContext context) at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__22.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at System.Web.Http.WebHost.HttpControllerHandler.WriteBufferedResponseContentAsync(HttpContextBase httpContextBase, HttpRequestMessage request, HttpResponseMessage response, IExceptionLogger exceptionLogger, IExceptionHandler exceptionHandler, CancellationToken cancellationToken) at System.Web.Http.WebHost.HttpControllerHandler.WriteResponseContentAsync(HttpContextBase httpContextBase, HttpRequestMessage request, HttpResponseMessage response, IExceptionLogger exceptionLogger, IExceptionHandler exceptionHandler, CancellationToken cancellationToken) at System.Web.Http.WebHost.HttpControllerHandler.<CopyResponseAsync>d__15.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at System.Web.Http.WebHost.HttpControllerHandler.CopyResponseAsync(HttpContextBase httpContextBase, HttpRequestMessage request, HttpResponseMessage response, IExceptionLogger exceptionLogger, IExceptionHandler exceptionHandler, CancellationToken cancellationToken) at System.Web.Http.WebHost.HttpControllerHandler.<ProcessRequestAsyncCore>d__12.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at System.Web.Http.WebHost.HttpControllerHandler.ProcessRequestAsyncCore(HttpContextBase contextBase) at System.Web.TaskAsyncHelper.BeginTask(Func1 taskFunc, AsyncCallback callback, Object state) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) at System.Web.HttpApplication.PipelineStepManager.ResumeSteps(Exception error) at System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) at System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags) at System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags) at System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion(IntPtr pHandler, RequestNotificationStatus& notificationStatus) at System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion(IntPtr pHandler, RequestNotificationStatus& notificationStatus) at System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags) at System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags)'

我原来的回答被否决并删除了,所以我会再试一次,希望这对某人有用:

  1. 回答问题"Has anyone else encountered this?"

then the answer is yes. My team has observed the same problem.

  1. 正如承诺的那样(在我删除的回复中...),我做了一些进一步的研究,发现 o
  2. 支持应用程序的 customErrors 模式

data error generator. You could set <customErrors mode="Off" /> in your web.config but that could expose dangerous data to your consumer, such as stack traces.

已提供更好的解决方案