FaultException<TDetail> Classe
Definizione
Utilizzata in un'applicazione client per rilevare errori SOAP specificati contrattualmente.Used in a client application to catch contractually-specified SOAP faults.
generic <typename TDetail>
public ref class FaultException : System::ServiceModel::FaultException
public class FaultException<TDetail> : System.ServiceModel.FaultException
[System.Serializable]
public class FaultException<TDetail> : System.ServiceModel.FaultException
type FaultException<'Detail> = class
inherit FaultException
[<System.Serializable>]
type FaultException<'Detail> = class
inherit FaultException
Public Class FaultException(Of TDetail)
Inherits FaultException
Parametri di tipo
- TDetail
Tipo di dettaglio dell'errore serializzabile.The serializable error detail type.
- Ereditarietà
- Ereditarietà
- Derivato
- Attributi
Esempio
Nell'esempio di codice seguente viene illustrato come un servizio utilizza il tipo FaultException<TDetail> per generare un'eccezione gestita che viene convertita nell'errore SOAP specificato da FaultContractAttribute.The following code example shows how a service uses the FaultException<TDetail> type to throw a managed exception that gets converted into the SOAP fault specified by the FaultContractAttribute.
using System;
using System.Collections.Generic;
using System.Net.Security;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace Microsoft.WCF.Documentation
{
[ServiceContract(Namespace="http://microsoft.wcf.documentation")]
public interface ISampleService{
[OperationContract]
[FaultContractAttribute(
typeof(GreetingFault),
Action="http://www.contoso.com/GreetingFault",
ProtectionLevel=ProtectionLevel.EncryptAndSign
)]
string SampleMethod(string msg);
}
[DataContractAttribute]
public class GreetingFault
{
private string report;
public GreetingFault(string message)
{
this.report = message;
}
[DataMemberAttribute]
public string Message
{
get { return this.report; }
set { this.report = value; }
}
}
class SampleService : ISampleService
{
#region ISampleService Members
public string SampleMethod(string msg)
{
Console.WriteLine("Client said: " + msg);
// Generate intermittent error behavior.
Random rnd = new Random(DateTime.Now.Millisecond);
int test = rnd.Next(5);
if (test % 2 != 0)
return "The service greets you: " + msg;
else
throw new FaultException<GreetingFault>(new GreetingFault("A Greeting error occurred. You said: " + msg));
}
#endregion
}
}
Imports System.Collections.Generic
Imports System.Net.Security
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.Text
Namespace Microsoft.WCF.Documentation
<ServiceContract(Namespace:="http://microsoft.wcf.documentation")> _
Public Interface ISampleService
<OperationContract, FaultContractAttribute(GetType(GreetingFault), Action:="http://www.contoso.com/GreetingFault", ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
Function SampleMethod(ByVal msg As String) As String
End Interface
<DataContractAttribute> _
Public Class GreetingFault
Private report As String
Public Sub New(ByVal message As String)
Me.report = message
End Sub
<DataMemberAttribute> _
Public Property Message() As String
Get
Return Me.report
End Get
Set(ByVal value As String)
Me.report = value
End Set
End Property
End Class
Friend Class SampleService
Implements ISampleService
#Region "ISampleService Members"
Public Function SampleMethod(ByVal msg As String) As String Implements ISampleService.SampleMethod
Console.WriteLine("Client said: " & msg)
' Generate intermittent error behavior.
Dim rand As New Random(DateTime.Now.Millisecond)
Dim test As Integer = rand.Next(5)
If test Mod 2 <> 0 Then
Return "The service greets you: " & msg
Else
Throw New FaultException(Of GreetingFault)(New GreetingFault("A Greeting error occurred. You said: " & msg))
End If
End Function
#End Region
End Class
End Namespace
Nell'esempio di codice seguente viene illustrato l'aspetto del codice client quando viene importato dal client utilizzando lo strumento ServiceModel Metadata Utility Tool (Svcutil.exe).The following code example shows how the client code looks when imported by the client using the ServiceModel Metadata Utility Tool (Svcutil.exe).
L'esempio di codice seguente mostra come un client può rilevare l'eccezione di tipo FaultException<TDetail> che rappresenta l'errore SOAP personalizzato specificato nel contratto di operazione.The following code example shows how a client can catch the FaultException<TDetail> type that represents the custom SOAP fault specified in the operation contract.
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Microsoft.WCF.Documentation;
public class Client
{
public static void Main()
{
// Picks up configuration from the config file.
SampleServiceClient wcfClient = new SampleServiceClient();
try
{
// Making calls.
Console.WriteLine("Enter the greeting to send: ");
string greeting = Console.ReadLine();
Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));
Console.WriteLine("Press ENTER to exit:");
Console.ReadLine();
// Done with service.
wcfClient.Close();
Console.WriteLine("Done!");
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
Console.ReadLine();
wcfClient.Abort();
}
catch (FaultException<GreetingFault> greetingFault)
{
Console.WriteLine(greetingFault.Detail.Message);
Console.ReadLine();
wcfClient.Abort();
}
catch (FaultException unknownFault)
{
Console.WriteLine("An unknown exception was received. " + unknownFault.Message);
Console.ReadLine();
wcfClient.Abort();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
Console.ReadLine();
wcfClient.Abort();
}
}
}
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports Microsoft.WCF.Documentation
Public Class Client
Public Shared Sub Main()
' Picks up configuration from the config file.
Dim wcfClient As New SampleServiceClient()
Try
' Making calls.
Console.WriteLine("Enter the greeting to send: ")
Dim greeting As String = Console.ReadLine()
Console.WriteLine("The service responded: " & wcfClient.SampleMethod(greeting))
Console.WriteLine("Press ENTER to exit:")
Console.ReadLine()
' Done with service.
wcfClient.Close()
Console.WriteLine("Done!")
Catch timeProblem As TimeoutException
Console.WriteLine("The service operation timed out. " & timeProblem.Message)
Console.ReadLine()
wcfClient.Abort()
Catch greetingFault As FaultException(Of GreetingFault)
Console.WriteLine(greetingFault.Detail.Message)
Console.ReadLine()
wcfClient.Abort()
Catch unknownFault As FaultException
Console.WriteLine("An unknown exception was received. " & unknownFault.Message)
Console.ReadLine()
wcfClient.Abort()
Catch commProblem As CommunicationException
Console.WriteLine("There was a communication problem. " & commProblem.Message + commProblem.StackTrace)
Console.ReadLine()
wcfClient.Abort()
End Try
End Sub
End Class
Commenti
Rilevare l' FaultException<TDetail> oggetto in un'applicazione client di Windows Communication Foundation (WCF) per gestire un errore SOAP che è stato specificato contrattualmente in un contratto dell'operazione.Catch the FaultException<TDetail> object in a Windows Communication Foundation (WCF) client application to handle a SOAP fault that has been contractually specified in an operation contract.
I servizi distribuiti in genere utilizzano l'attributo FaultContractAttribute per specificare formalmente tutti gli errori SOAP che un client prevede di ricevere durante il normale funzionamento di un'operazione.Typical deployed services use the FaultContractAttribute to formally specify all SOAP faults that a client can expect to receive in the normal course of an operation. Le informazioni sull'errore in un FaultContractAttribute FaultException<TDetail> oggetto vengono visualizzate come (dove typeparameter è l'oggetto errore serializzabile specificato nell'operazione FaultContractAttribute ) quando arriva a un'applicazione client.Error information in a FaultContractAttribute appears as a FaultException<TDetail> (where the typeparameter is the serializable error object specified in the operation's FaultContractAttribute) when it arrives at a client application. L'attributo FaultContractAttribute può essere utilizzato per specificare errori SOAP sia per i metodi di servizio bidirezionali sia per le coppie di metodi asincroni.The FaultContractAttribute can be used to specify SOAP faults for both two-way service methods and for asynchronous method pairs.
Poiché l'eccezione FaultException<TDetail> è un'eccezione FaultException e pertanto un'eccezione CommunicationException, il rilevamento degli errori SOAP specificati garantisce che le eccezioni di tipo FaultException<TDetail> vengano rilevate prima delle eccezioni di tipo FaultException e CommunicationException oppure garantisce che gli errori specificati vengano gestiti mediante un gestore di tali eccezioni.Because FaultException<TDetail> is both a FaultException and therefore a CommunicationException, to catch specified SOAP faults make sure you catch the FaultException<TDetail> types prior to the FaultException and CommunicationException types or handle the specified exceptions in one of those exception handlers.
Nota
Se si utilizza un attributo System.ServiceModel.FaultContractAttribute per specificare un'eccezione FaultException<TDetail> in cui il parametro di tipo è una stringa System.String, il valore della stringa viene assegnato alla proprietà Detail dell'applicazione client. I client non sono in grado di recuperare tale stringa chiamando il metodo FaultException<TDetail>.ToString.If you use System.ServiceModel.FaultContractAttribute to specify a FaultException<TDetail> where the type parameter is a System.String, the string value is assigned to the Detail property in the client application; clients cannot retrieve that string by calling the FaultException<TDetail>.ToString method. Per fare in modo che il valore della stringa venga restituito quando l'applicazione client chiama il metodo Exception.ToString è sufficiente generare un'eccezione System.ServiceModel.FaultException nell'operazione e passare la stringa al costruttore.To have the string value returned when the client application calls Exception.ToString, throw a System.ServiceModel.FaultException exception inside the operation and pass the string to the constructor.
In generale è consigliabile che i tipi di dettaglio siano tipi serializzabili personalizzati appropriati all'errore e non una stringa System.String.In general, it is recommended that detail types be custom serializable types appropriate to the fault and not a System.String.
Costruttori
FaultException<TDetail>(SerializationInfo, StreamingContext) |
Consente di inizializzare una nuova istanza della classe FaultException<TDetail> utilizzando le informazioni e il contesto di serializzazione specificati quando si deserializza un flusso in un oggetto FaultException.Initializes a new instance of the FaultException<TDetail> class using the specified serialization information and context when deserializing a stream into a FaultException object. |
FaultException<TDetail>(TDetail) |
Consente di inizializzare una nuova istanza della classe FaultException<TDetail> che utilizza l'oggetto dettaglio specificato.Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object. |
FaultException<TDetail>(TDetail, FaultReason) |
Consente di inizializzare una nuova istanza della classe FaultException<TDetail> che utilizza l'oggetto dettaglio e il motivo dell'errore specificati.Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object and fault reason. |
FaultException<TDetail>(TDetail, FaultReason, FaultCode) |
Consente di inizializzare una nuova istanza della classe FaultException<TDetail> che utilizza l'oggetto dettaglio, il motivo e il codice dell'errore specificati.Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, fault reason, and fault code. |
FaultException<TDetail>(TDetail, FaultReason, FaultCode, String) |
Consente di inizializzare una nuova istanza della classe FaultException<TDetail> che utilizza l'oggetto dettaglio, il motivo, il codice e i valori di azione dell'errore SOAP specificati.Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, and SOAP fault reason, code and action values. |
FaultException<TDetail>(TDetail, String) |
Consente di inizializzare una nuova istanza della classe FaultException<TDetail> che utilizza il dettaglio e il motivo dell'errore specificati.Initializes a new instance of the FaultException<TDetail> class that uses the specified detail and fault reason. |
FaultException<TDetail>(TDetail, String, FaultCode) |
Consente di inizializzare una nuova istanza della classe FaultException<TDetail> che utilizza l'oggetto dettaglio, il motivo e il codice dell'errore specificati.Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, fault reason, and fault code. |
FaultException<TDetail>(TDetail, String, FaultCode, String) |
Consente di inizializzare una nuova istanza della classe FaultException<TDetail> che utilizza l'oggetto dettaglio, il motivo, il codice e i valori di azione dell'errore SOAP specificati.Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, and SOAP fault reason, code and action values. |
Proprietà
Action |
Consente di ottenere il valore di azione SOAP relativo al messaggio di errore.Gets the value of the SOAP action for the fault message. (Ereditato da FaultException) |
Code |
Consente di ottenere il codice dell'errore SOAP.Gets the fault code for the SOAP fault. (Ereditato da FaultException) |
Data |
Ottiene una raccolta di coppie chiave/valore che forniscono informazioni definite dall'utente aggiuntive sull'eccezione.Gets a collection of key/value pairs that provide additional user-defined information about the exception. (Ereditato da Exception) |
Detail |
Consente di ottenere un oggetto contenente le informazioni dettagliate sulla condizione di errore.Gets the object that contains the detail information of the fault condition. |
HelpLink |
Ottiene o imposta un collegamento al file della Guida associato all'eccezione.Gets or sets a link to the help file associated with this exception. (Ereditato da Exception) |
HResult |
Ottiene o imposta HRESULT, un valore numerico codificato che viene assegnato a un'eccezione specifica.Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Ereditato da Exception) |
InnerException |
Ottiene l'istanza di Exception che ha causato l'eccezione corrente.Gets the Exception instance that caused the current exception. (Ereditato da Exception) |
Message |
Consente di ottenere il messaggio dell'eccezione.Gets the message for the exception. (Ereditato da FaultException) |
Reason |
Consente di ottenere l'oggetto FaultReason relativo all'errore SOAP.Gets the FaultReason for the SOAP fault. (Ereditato da FaultException) |
Source |
Ottiene o imposta il nome dell'oggetto o dell'applicazione che ha generato l'errore.Gets or sets the name of the application or the object that causes the error. (Ereditato da Exception) |
StackTrace |
Ottiene una rappresentazione di stringa dei frame immediati nello stack di chiamate.Gets a string representation of the immediate frames on the call stack. (Ereditato da Exception) |
TargetSite |
Ottiene il metodo che genera l'eccezione corrente.Gets the method that throws the current exception. (Ereditato da Exception) |
Metodi
CreateMessageFault() |
Consente di creare un oggetto MessageFault utilizzabile per creare un messaggio Message che rappresenta l'errore SOAP.Creates a MessageFault object that can be used to create a Message that represents the SOAP fault. |
Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object. (Ereditato da Object) |
GetBaseException() |
Quando ne viene eseguito l'override in una classe derivata, restituisce l'Exception che è la causa radice di una o più eccezioni successive.When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Ereditato da Exception) |
GetHashCode() |
Funge da funzione hash predefinita.Serves as the default hash function. (Ereditato da Object) |
GetObjectData(SerializationInfo, StreamingContext) |
Implementazione del metodo GetObjectData(SerializationInfo, StreamingContext) chiamato quando l'oggetto viene serializzato in un flusso.Implementation of the GetObjectData(SerializationInfo, StreamingContext) method that is called when the object is serialized into a stream. |
GetObjectData(SerializationInfo, StreamingContext) |
Implementazione del metodo GetObjectData(SerializationInfo, StreamingContext) chiamato quando l'oggetto viene serializzato in un flusso.Implementation of the GetObjectData(SerializationInfo, StreamingContext) method that is called when the object is serialized into a stream. (Ereditato da FaultException) |
GetType() |
Ottiene il tipo di runtime dell'istanza corrente.Gets the runtime type of the current instance. (Ereditato da Exception) |
MemberwiseClone() |
Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object. (Ereditato da Object) |
ToString() |
Restituisce una stringa che rappresenta l'oggetto FaultException<TDetail>.Returns a string for the FaultException<TDetail> object. |
Eventi
SerializeObjectState |
Si verifica quando un'eccezione viene serializzata per creare un oggetto di stato eccezione contenente i dati serializzati relativi all'eccezione.Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Ereditato da Exception) |