簡體   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