2014-12-02 171 views
0

如果我对数据进行反序列化并在更新过程中稍后发生错误(例如:更改无法满足数据库约束)。我想在这里向最终用户报告一些有用的东西,但看起来.NET正在吞噬这个错误。任何想法为什么?ASP.NET Web服务返回400

<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 
+0

很可能你没有正确地调用你的web服务。调用代码在哪里? – Dalorzo 2014-12-02 15:51:53

+0

另外,我强烈建议你使用'ex.ToString()'而不是ex.Message和ex.StackTrace。 – 2014-12-02 21:52:25

回答

0

很显然,这会导致问题,如果你的代码有冒然通过引发异常,打破正常流动的底层流对象的StreamReader的处置。

所以,现在我使用OperationContext.Current.RequestContext.RequestMessage.ToString,它有它自己的问题,但至少我没有选择错误记录或资源泄漏。