Condividi tramite


ExceptionHandler Classe

Definizione

Estendere la ExceptionHandler classe per creare un gestore eccezioni per le eccezioni non gestite che si verificano all'interno del runtime di Windows Communication Foundation (WCF).

public ref class ExceptionHandler abstract
public abstract class ExceptionHandler
type ExceptionHandler = class
Public MustInherit Class ExceptionHandler
Ereditarietà
ExceptionHandler

Esempio

Nell'esempio di codice seguente viene illustrata un'implementazione della ExceptionHandler classe astratta che esegue l'override del HandleException metodo .

using System;
using System.ServiceModel.Dispatcher;

namespace CS
{
    public class MyExceptionHandler : ExceptionHandler
    {
        // HandleException method override gives control to
        // your code.
        public override bool HandleException(Exception ex)
        {
            // This method contains logic to decide whether
            // the exception is serious enough
            // to terminate the process.
            return ShouldTerminateProcess(ex);
        }

        public bool ShouldTerminateProcess(Exception ex)
        {
            // Write your logic here.
            return true;
        }
    }


Imports System.ServiceModel.Dispatcher

Namespace CS
    Public Class MyExceptionHandler
        Inherits ExceptionHandler
            ' HandleException method override gives control to 
            ' your code.
            Public Overrides Function HandleException(ByVal ex As Exception) As Boolean
                ' This method contains logic to decide whether 
                ' the exception is serious enough
                ' to terminate the process.
                Return ShouldTerminateProcess (ex)
            End Function

            Public Function ShouldTerminateProcess(ByVal ex As Exception) As Boolean
                ' Write your logic here.
                Return True
            End Function
    End Class

Nell'esempio di codice seguente viene illustrato come abilitare l'oggetto personalizzato MyExceptionHandler per le eccezioni non gestite che si verificano all'interno del runtime WCF.

// Create an instance of the MyExceptionHandler class.
MyExceptionHandler thisExceptionHandler =
    new MyExceptionHandler();

// Enable the custom handler by setting
//   AsynchronousThreadExceptionHandler property
//   to this object.
ExceptionHandler.AsynchronousThreadExceptionHandler =
    thisExceptionHandler;

// After the handler is set, write your call to
// System.ServiceModel.ICommunication.Open here.
    Sub Main(ByVal args() As String)
        ' Create an instance of the MyExceptionHandler class.
        Dim thisExceptionHandler As New MyExceptionHandler()

        ' Enable the custom handler by setting 
        '   AsynchronousThreadExceptionHandler property
        '   to this object.
        ExceptionHandler.AsynchronousThreadExceptionHandler = thisExceptionHandler

        ' After the handler is set, write your call to 
        ' System.ServiceModel.ICommunication.Open here
    End Sub
End Module

Commenti

Estendere la classe ed eseguire l'override ExceptionHandler del HandleException metodo per determinare se un'eccezione deve terminare l'applicazione. Creare quindi una nuova istanza della classe personalizzata ExceptionHandler e assegnarla alla proprietà o TransportExceptionHandler statica AsynchronousThreadExceptionHandler prima di creare client o servizi WCF.

Costruttori

Nome Descrizione
ExceptionHandler()

Inizializza una nuova istanza della classe ExceptionHandler.

Proprietà

Nome Descrizione
AlwaysHandle

Ottiene un'istanza di ExceptionHandler che gestisce tutte le eccezioni.

AsynchronousThreadExceptionHandler

Ottiene o imposta l'implementazione corrente ExceptionHandler per il dominio applicazione.

TransportExceptionHandler

Ottiene o imposta l'implementazione del trasporto ExceptionHandler corrente per il dominio applicazione.

Metodi

Nome Descrizione
Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
HandleException(Exception)

Quando sottoposto a override in una classe derivata, restituisce true se l'eccezione è stata gestita o false se l'eccezione deve essere nuovamente generata e l'applicazione è stata terminata.

MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a