How to better debug a WebService asmx -- 500 Internal Server Error
I need the errors from my web service sent directly to the client in Json format so I can read exactly what error is being thrown. How do I do this please?
This is what I keep getting in Fiddler
Runtime Error
Description: An application error occurred on the server. The current 开发者_JAVA百科custom error settings for this application prevent the details of the application error from being viewed.
Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly"/>
</system.web>
</configuration>
Server code
<WebMethod(EnableSession:=True)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Protected Function PopulateUsers() As IDictionary(Of String, String)
Dim users As New Dictionary(Of String, String)
Try
Dim arry As ArrayList = UserController.GetUsers(0, True)
For Each objUser As Users In arry
userrole.Add(CStr(objUser.UserID), objUser)
Next
Catch ex As Exception
Dim ur As New Dictionary(Of String, String)
Dim s As New User
s.DisplayName = ex.GetBaseException.StackTrace
s.UserID = 1
users.Add(ex.GetBaseException.Message, s)
Return s
End Try
Return users
End Function
Make sure you have proper error handling in your web service. In case of any error, convert your error object into a well formed json object and send it. In the client side, success handler will only be invoked but you need to check whether the response contains the error object or the actual response and act accordingly.
There is error in your catch block, try this
Protected Function PopulateUsers() As IDictionary(Of String, String)
Dim users As New Dictionary(Of String, String)
Try
Dim arry As ArrayList = UserController.GetUsers(0, True)
For Each objUser As Users In arry
userrole.Add(CStr(objUser.UserID), objUser)
Next
Catch ex As Exception
Dim ur As New Dictionary(Of String, String)
ur.Add("ERROR", ex.GetBaseException.StackTrace)
Return ur
End Try
Return users
End Function
Use elmah to get a full set of error logging with reports and alerts.
精彩评论