ExceptionHandler Clase

Definición

Extienda la clase ExceptionHandler para crear un controlador de excepciones para las excepciones no controladas que tengan lugar dentro del tiempo de ejecución de Windows Communication Foundation (WCF).

public ref class ExceptionHandler abstract
public abstract class ExceptionHandler
type ExceptionHandler = class
Public MustInherit Class ExceptionHandler
Herencia
ExceptionHandler

Ejemplos

En el ejemplo de código siguiente se muestra una implementación de la clase abstracta ExceptionHandler que invalida el método 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

En el ejemplo de código siguiente se muestra cómo habilitar el personalizado MyExceptionHandler para excepciones no controladas que se producen en el tiempo de ejecución de 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

Comentarios

Extienda la clase ExceptionHandler e invalide el método HandleException para determinar si una excepción debe finalizar la aplicación. A continuación, cree una nueva instancia de la clase personalizada ExceptionHandler y asígnela a la propiedad estática AsynchronousThreadExceptionHandler o TransportExceptionHandler antes de crear clientes o servicios WCF.

Constructores

ExceptionHandler()

Inicializa una nueva instancia de la clase ExceptionHandler.

Propiedades

AlwaysHandle

Obtiene una instancia de ExceptionHandler que controla todas las excepciones.

AsynchronousThreadExceptionHandler

Obtiene o establece la implementación de ExceptionHandler actual para el dominio de la aplicación.

TransportExceptionHandler

Obtiene o establece la implementación de transporte de ExceptionHandler para el dominio de la aplicación.

Métodos

Equals(Object)

Determina si el objeto especificado es igual que el objeto actual.

(Heredado de Object)
GetHashCode()

Sirve como la función hash predeterminada.

(Heredado de Object)
GetType()

Obtiene el Type de la instancia actual.

(Heredado de Object)
HandleException(Exception)

Si se invalida en una clase derivada, devuelve true si se ha controlado la excepción, o false si la excepción se debe volver a iniciar y la aplicación se debe finalizar.

MemberwiseClone()

Crea una copia superficial del Object actual.

(Heredado de Object)
ToString()

Devuelve una cadena que representa el objeto actual.

(Heredado de Object)

Se aplica a