Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

User Friendly ASP.NET Exception Handling

, 20 Dec 2004
A flexible framework for user-friendly ASP.NET exception handling, and automatically notifying developers of problems before users do.
ASPNETExceptionHandling_demo.zip
WebServiceTestConsoleApplication
bin
Web References
WebReference
Reference.map
service1.disco
service1.wsdl
ASPUnhandledException
bin
Imports System.IO
Imports System.Web.Services
Imports System.Web.Services.Protocols

''' <summary>
''' Implements ASP unhandled exception manager as a SoapExtension
''' </summary>
''' <remarks>
''' to use:
''' 
'''    1) place ASPUnhandledException.dll in the \bin folder
'''    2) add this section to your Web.config under the &lt;webServices&gt; element:
'''			&lt;!-- Adds our error handler to the SOAP pipeline. --&gt;
'''		    &lt;soapExtensionTypes&gt;
'''				&lt;add type="ASPUnhandledException.UehSoapExtension, ASPUnhandledException"
'''				     priority="1" group="0" /&gt;
'''			&lt;/soapExtensionTypes&gt;
'''
'''  Jeff Atwood
'''  http://www.codinghorror.com/
''' </remarks>
Public Class UehSoapExtension
    Inherits SoapExtension

    Private _OldStream As Stream
    Private _NewStream As Stream

    Public Overloads Overrides Function GetInitializer(ByVal serviceType As System.Type) As Object
        Return Nothing
    End Function

    Public Overloads Overrides Function GetInitializer(ByVal methodInfo As System.Web.Services.Protocols.LogicalMethodInfo, ByVal attribute As System.Web.Services.Protocols.SoapExtensionAttribute) As Object
        Return Nothing
    End Function

    Public Overrides Sub Initialize(ByVal initializer As Object)
    End Sub

    Public Overrides Function ChainStream(ByVal stream As Stream) As Stream
        _OldStream = stream
        _NewStream = New MemoryStream
        Return _NewStream
    End Function

    Private Sub Copy(ByVal fromStream As Stream, ByVal toStream As Stream)
        Dim sr As New StreamReader(fromStream)
        Dim sw As New StreamWriter(toStream)
        sw.Write(sr.ReadToEnd())
        sw.Flush()
    End Sub

    Public Overrides Sub ProcessMessage(ByVal message As System.Web.Services.Protocols.SoapMessage)

        Select Case message.Stage
            Case SoapMessageStage.BeforeDeserialize
                Copy(_OldStream, _NewStream)
                _NewStream.Position = 0

            Case SoapMessageStage.AfterSerialize
                If Not message.Exception Is Nothing Then
                    Dim ueh As New Handler
                    Dim strDetailNode As String
                    '-- handle our exception, and get the SOAP <detail> string
                    strDetailNode = ueh.HandleWebServiceException(message)
                    '-- read the entire SOAP message stream into a string
                    _NewStream.Position = 0
                    Dim tr As TextReader = New StreamReader(_NewStream)
                    '-- insert our exception details into the string
                    Dim s As String = tr.ReadToEnd
                    s = s.Replace("<detail />", strDetailNode)
                    '-- overwrite the stream with our modified string
                    _NewStream = New MemoryStream
                    Dim tw As TextWriter = New StreamWriter(_NewStream)
                    tw.Write(s)
                    tw.Flush()
                End If
                _NewStream.Position = 0
                Copy(_NewStream, _OldStream)

        End Select

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

wumpus1
Web Developer
United States United States
My name is Jeff Atwood. I live in Berkeley, CA with my wife, two cats, and far more computers than I care to mention. My first computer was the Texas Instruments TI-99/4a. I've been a Microsoft Windows developer since 1992; primarily in VB. I am particularly interested in best practices and human factors in software development, as represented in my recommended developer reading list. I also have a coding and human factors related blog at www.codinghorror.com.

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 21 Dec 2004
Article Copyright 2004 by wumpus1
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid