ExceptionHandler Classe

Définition

Étendez la classe ExceptionHandler pour créer un gestionnaire d’exceptions pour les exceptions non gérées qui se produisent dans le runtime Windows Communication Foundation (WCF).

public ref class ExceptionHandler abstract
public abstract class ExceptionHandler
type ExceptionHandler = class
Public MustInherit Class ExceptionHandler
Héritage
ExceptionHandler

Exemples

L'exemple de code suivant présente une implémentation de la classe abstraite ExceptionHandler qui substitue la méthode HandleException.

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

L’exemple de code suivant montre comment activer la fonctionnalité personnalisée MyExceptionHandler pour les exceptions non gérées qui se produisent dans le runtime WCF.

    static void Main(string[] args)
    {
        // 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

Remarques

Étendez la classe ExceptionHandler et substituez la méthode HandleException pour déterminer si une exception doit fermer l'application. Créez ensuite une instance de votre classe personnalisée ExceptionHandler et affectez-la à la propriété ou TransportExceptionHandler statique AsynchronousThreadExceptionHandler avant de créer des clients ou des services WCF.

Constructeurs

ExceptionHandler()

Initialise une nouvelle instance de la classe ExceptionHandler.

Propriétés

AlwaysHandle

Obtient une instance de ExceptionHandler qui gère toutes les exceptions.

AsynchronousThreadExceptionHandler

Obtient ou définit l'implémentation du ExceptionHandler actuel pour le domaine d'application.

TransportExceptionHandler

Obtient ou définit l'implémentation du ExceptionHandler de transport actuel pour le domaine d'application.

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
HandleException(Exception)

En cas de substitution dans une classe dérivée, retourne true si l'exception a été gérée ou false si l'exception doit levée une nouvelle fois et l'application fermée.

MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à