更新 : 2007 年 11 月
コントラクトで指定された SOAP エラーをキャッチするためにクライアント アプリケーションで使用されます。
名前空間 :
System.ServiceModel
アセンブリ :
System.ServiceModel (System.ServiceModel.dll 内)
<SerializableAttribute> _
Public Class FaultException(Of TDetail) _
Inherits FaultException
Dim instance As FaultException(Of TDetail)
[SerializableAttribute]
public class FaultException<TDetail> : FaultException
[SerializableAttribute]
generic<typename TDetail>
public ref class FaultException : public FaultException
J# では、ジェネリック API は使用できますが、新規に宣言することはできません。
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>)>) 型を必ずキャッチするか、指定された例外を例外ハンドラのいずれかで必ず処理します。
次のコード例では、FaultContractAttribute により指定された SOAP エラーに変換されるマネージ例外をスローするために、サービスが FaultException<(Of <(TDetail>)>) 型を使用する方法を示しています。
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>)>) 型をクライアントがキャッチできる方法を示しています。
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
参照