System.ServiceModel 名前空間


.NET Framework クラス ライブラリ
FaultException クラス

更新 : 2007 年 11 月

SOAP エラーを表します。

名前空間 :  System.ServiceModel
アセンブリ :  System.ServiceModel (System.ServiceModel.dll 内)
構文

Visual Basic (宣言)
<SerializableAttribute> _
Public Class FaultException _
    Inherits CommunicationException
Visual Basic (使用法)
Dim instance As FaultException
C#
[SerializableAttribute]
public class FaultException : CommunicationException
Visual C++
[SerializableAttribute]
public ref class FaultException : public CommunicationException
J#
/** @attribute SerializableAttribute */ 
public class FaultException extends CommunicationException
JScript
public class FaultException extends CommunicationException
解説

サービスでは、FaultException クラスを使用して、デバッグ用にクライアントに返す型指定のないエラーを作成します。

クライアントでは、FaultException オブジェクトをキャッチして、IncludeExceptionDetailInFaults プロパティを true に設定してサービスから返された不明なエラーや一般的なエラーを処理します。FaultExceptionCommunicationException を拡張するため、別々にキャッチする場合は、CommunicationException オブジェクトをキャッチする前に FaultException オブジェクトをキャッチしてください。

ms576192.alert_note(ja-jp,VS.90).gifメモ :

双方向サービスでも、双方向クライアントとの対話で返される FaultException オブジェクトをキャッチできます。

通常は、FaultContractAttribute を使用して、クライアントにエラー情報が必要であると判断されるすべてのエラー ケースに対して (マネージ例外オブジェクトではなく) 厳密に型指定された SOAP エラーを返すサービスを設計することを強くお勧めします。ただし、次の状況の場合は、FaultException を使用します。

  • デバッグ用にサービスから SOAP エラーを送信する。

  • エラーがサービスコントラクトの一部ではない場合に、クライアント上で SOAP エラーをキャッチする。

文字列をコンストラクタに渡し、クライアントが FaultException<(Of <(TDetail>)>)..::.ToString メソッドを呼び出すことでその文字列を取得する場合は、FaultException オブジェクトをスローします。型パラメータが System..::.String である System.ServiceModel..::.FaultException<(Of <(TDetail>)>) 型のエラー コントラクトを指定した場合、文字列値は FaultException<(Of <(TDetail>)>)..::.ToString を呼び出さなくても FaultException<(Of <(TDetail>)>)..::.Detail プロパティとして取得できます。

詳細については、「コントラクトおよびサービスのエラーの指定と処理」を参照してください。


次のコード例は、サービスからスローされた FaultException オブジェクトをキャッチして処理する try/catch ブロックの使用方法を示しています。これは、サービス アプリケーションでデバッグがオンの場合にしばしば使用される方法です。

C#
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
参照

参照

タグ :


Page view tracker