更新 : 2007 年 11 月
名前空間 :
System.ServiceModel
アセンブリ :
System.ServiceModel (System.ServiceModel.dll 内)
<SerializableAttribute> _
Public Class FaultException _
Inherits CommunicationException
Dim instance As FaultException
[SerializableAttribute]
public class FaultException : CommunicationException
[SerializableAttribute]
public ref class FaultException : public CommunicationException
/** @attribute SerializableAttribute */
public class FaultException extends CommunicationException
public class FaultException extends CommunicationException
サービスでは、FaultException クラスを使用して、デバッグ用にクライアントに返す型指定のないエラーを作成します。
クライアントでは、FaultException オブジェクトをキャッチして、IncludeExceptionDetailInFaults プロパティを true に設定してサービスから返された不明なエラーや一般的なエラーを処理します。FaultException は CommunicationException を拡張するため、別々にキャッチする場合は、CommunicationException オブジェクトをキャッチする前に FaultException オブジェクトをキャッチしてください。
メモ : |
|---|
双方向サービスでも、双方向クライアントとの対話で返される FaultException オブジェクトをキャッチできます。 |
通常は、FaultContractAttribute を使用して、クライアントにエラー情報が必要であると判断されるすべてのエラー ケースに対して (マネージ例外オブジェクトではなく) 厳密に型指定された SOAP エラーを返すサービスを設計することを強くお勧めします。ただし、次の状況の場合は、FaultException を使用します。
文字列をコンストラクタに渡し、クライアントが FaultException<(Of <(TDetail>)>)..::.ToString メソッドを呼び出すことでその文字列を取得する場合は、FaultException オブジェクトをスローします。型パラメータが System..::.String である System.ServiceModel..::.FaultException<(Of <(TDetail>)>) 型のエラー コントラクトを指定した場合、文字列値は FaultException<(Of <(TDetail>)>)..::.ToString を呼び出さなくても FaultException<(Of <(TDetail>)>)..::.Detail プロパティとして取得できます。
詳細については、「コントラクトおよびサービスのエラーの指定と処理」を参照してください。
次のコード例は、サービスからスローされた FaultException オブジェクトをキャッチして処理する try/catch ブロックの使用方法を示しています。これは、サービス アプリケーションでデバッグがオンの場合にしばしば使用される方法です。
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 configuration 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();
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
wcfClient.Abort();
Console.ReadLine();
}
// Catch the contractually specified SOAP fault raised here as an exception.
catch (FaultException<GreetingFault> greetingFault)
{
Console.WriteLine(greetingFault.Detail.Message);
Console.Read();
wcfClient.Abort();
}
// Catch unrecognized faults. This handler receives exceptions thrown by WCF
// services when ServiceDebugBehavior.IncludeExceptionDetailInFaults
// is set to true.
catch (FaultException faultEx)
{
Console.WriteLine("An unknown exception was received. "
+ faultEx.Message
+ faultEx.StackTrace
);
Console.Read();
wcfClient.Abort();
}
// Standard communication fault handler.
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
Console.Read();
wcfClient.Abort();
}
}
}
System..::.Object
System..::.Exception
System..::.SystemException
System.ServiceModel..::.CommunicationException
System.ServiceModel..::.FaultException
System.ServiceModel..::.FaultException<(Of <(TDetail>)>)
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
Windows Vista, Windows XP SP2, Windows Server 2003
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0
参照