繁体   English   中英

ASP.NET Web服务返回400

[英]ASP.NET web service returns 400

如果我对数据进行反序列化,然后在更新过程中稍后发生错误(例如:更改不符合数据库约束)。 我想在此向最终用户报告一些有用的信息,但看起来.NET正在吞噬该错误。 知道为什么吗?

<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

显然,StreamReader处置其基础Stream对象,如果您的代码具有通过引发异常来中断正常流的临时性,则会导致问题。

因此,现在我正在使用OperationContext.Current.RequestContext.RequestMessage.ToString,它有其自身的问题,但至少我没有在混乱的错误日志记录或资源泄漏之间进行选择。

暂无
暂无

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

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