简体   繁体   English

ASP.NET Web服务返回400

[英]ASP.NET web service returns 400

If I de-serialize the data and an error occurs later in the update process (for example: the change fails to meet a database constraint). 如果我对数据进行反序列化,然后在更新过程中稍后发生错误(例如:更改不符合数据库约束)。 I'd like to report something useful to the end user here, but it looks like .NET is swallowing the error. 我想在此向最终用户报告一些有用的信息,但看起来.NET正在吞噬该错误。 Any idea why? 知道为什么吗?

<WebInvoke(method:="POST", uriTemplate:="changes", bodyStyle:=WebMessageBodyStyle.Bare)>
Public Function PostChanges(body As Stream) As String
    Try
        ' This returns 500 Server Error (with wrapped exception)
        'Throw New Exception("TEST")

        Dim data As String = ""
        Using reader As New StreamReader(body)
            data = reader.ReadToEnd()
        End Using

        ' This returns 400 Bad Request
        'Throw New Exception("TEST")

        ' Code handling data removed (no error if exceptions are removed)
    Catch ex As Exception
        Throw New WebFaultException(Of DisplayError)(New DisplayError(ex.Message, ex.StackTrace), Net.HttpStatusCode.InternalServerError)
    End Try

Apparently, StreamReader disposes of its underlying Stream object which causes problem if your code has the temerity to break normal flow by raising an exception. 显然,StreamReader处置其基础Stream对象,如果您的代码具有通过引发异常来中断正常流的临时性,则会导致问题。

So, now I'm using OperationContext.Current.RequestContext.RequestMessage.ToString, which has its own problems but at least I'm not choosing between messed up error logging or a resource leak. 因此,现在我正在使用OperationContext.Current.RequestContext.RequestMessage.ToString,它有其自身的问题,但至少我没有在混乱的错误日志记录或资源泄漏之间进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM