クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
.NET Framework 3.5
.NET Framework 3.5
System.ServiceModel 名前空間
FaultException(TDetail) クラス

  低帯域幅での表示をオンにする
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2008/.NET Framework 3.5

その他のバージョンについては、以下の情報を参照してください。
.NET Framework クラス ライブラリ
FaultException<(Of <(TDetail>)>) クラス

更新 : 2007 年 11 月

コントラクトで指定された SOAP エラーをキャッチするためにクライアント アプリケーションで使用されます。

名前空間 :  System.ServiceModel
アセンブリ :  System.ServiceModel (System.ServiceModel.dll 内)
Visual Basic (宣言)
<SerializableAttribute> _
Public Class FaultException(Of TDetail) _
    Inherits FaultException
Visual Basic (使用法)
Dim instance As FaultException(Of TDetail)
C#
[SerializableAttribute]
public class FaultException<TDetail> : FaultException
Visual C++
[SerializableAttribute]
generic<typename TDetail>
public ref class FaultException : public FaultException
J#
J# では、ジェネリック API は使用できますが、新規に宣言することはできません。
JScript
JScript では、ジェネリックな型またはメソッドは使用できません。

型パラメータ

TDetail

シリアル化可能なエラーの詳細な型。

Windows Communication Foundation (WCF) クライアント アプリケーション内で FaultException<(Of <(TDetail>)>) オブジェクトをキャッチして、操作コントラクトの中に指定されている SOAP エラーを処理します。

通常使用されるサービスでは、FaultContractAttribute を使用して、通常の操作中に受信することをクライアントが予期できるすべての SOAP エラーを正式に指定します。FaultContractAttribute 内のエラー情報は、それがクライアント アプリケーションに到着したときに、FaultException<(Of <(TDetail>)>) として表示されます (型パラメータは、操作の FaultContractAttribute に指定されたシリアル化可能なエラー オブジェクトです)。FaultContractAttribute を使用して、双方向サービス メソッドと非同期メソッド ペアの SOAP エラーを両方とも指定できます。

FaultException<(Of <(TDetail>)>)FaultException であり、したがって CommunicationException でもあるので、指定された SOAP エラーをキャッチするには、FaultException 型と CommunicationException 型をキャッチする前に FaultException<(Of <(TDetail>)>) 型を必ずキャッチするか、指定された例外を例外ハンドラのいずれかで必ず処理します。

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

System.ServiceModel..::.FaultContractAttribute を使用して、型パラメータが System..::.String である FaultException<(Of <(TDetail>)>) を指定した場合、クライアント アプリケーションでは文字列値に Detail プロパティが割り当てられ、クライアントは FaultException<(Of <(TDetail>)>)..::.ToString メソッドを呼び出してもその文字列を取得できません。クライアント アプリケーションが Exception..::.ToString を呼び出したときにこの文字列値を返すには、操作内で System.ServiceModel..::.FaultException 例外をスローし、この文字列をコンストラクタに渡します。通常は、詳細な型はエラーに適したシリアル化可能なカスタム型にし、System..::.String にはしないことをお勧めします。

次のコード例では、FaultContractAttribute により指定された SOAP エラーに変換されるマネージ例外をスローするために、サービスが FaultException<(Of <(TDetail>)>) 型を使用する方法を示しています。

C#
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 rand = new Random(DateTime.Now.Millisecond);
    int test = rand.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
  }
}

次のコード例では、クライアントが ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用してインポートしたときのクライアント コードがどのようになるのかを示しています。

次のコード例は、操作コントラクト内に指定されたカスタム SOAP エラーを表す FaultException<(Of <(TDetail>)>) 型をクライアントがキャッチできる方法を示しています。

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 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();
    }
  }
}

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
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件  |  商標  |  プライバシー
Page view tracker