OData错误响应丢失异常细节C#

时间:2018-04-20 08:12:53

标签: c# azure asp.net-web-api2 odata azure-web-sites

我在我的localhost机器上运行了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?

  • 我尝试在调试模式下运行代码
  • 将dll从我的本地计算机物理复制到azure,以防它是nuget包的问题
  • 覆盖默认错误序列化程序
  • 禁用/启用任何记录器,非默认媒体格式化程序

我有一个异常记录器可以很好地获取异常(在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 个答案:

答案 0 :(得分:2)

我的原始答案已被否决并删除,因此我将它改一遍,希望这对某人有用:

  1. 在回答“其他人遇到过这个问题吗?”的问题
  

那么答案是肯定的。我的团队也遇到了同样的问题。

  1. 按照承诺(在我删除的回复中...),我进行了进一步的研究,发现该应用程序的customErrors模式已被o认可。
  

数据错误生成器。您可以设置<customErrors mode="Off" />   在您的web.config中,但这可能会将危险数据暴露给您   消费者,例如堆栈跟踪。

here提供了更好的解决方案。