サービス メソッドの途中で例外から返されるカスタム
FaultException を作成できます。
名前空間: System.ServiceModel.Dispatcher
アセンブリ: System.ServiceModel (system.servicemodel.dll 内)
Sub ProvideFault ( _
error As Exception, _
version As MessageVersion, _
ByRef fault As Message _
)
Dim instance As IErrorHandler
Dim error As Exception
Dim version As MessageVersion
Dim fault As Message
instance.ProvideFault(error, version, fault)
void ProvideFault (
Exception error,
MessageVersion version,
ref Message fault
)
void ProvideFault (
Exception^ error,
MessageVersion^ version,
Message^% fault
)
void ProvideFault (
Exception error,
MessageVersion version,
/** @ref */ Message fault
)
パラメータ
- error
サービス操作の過程でスローされる Exception オブジェクト。
- version
SOAP バージョンのメッセージ。
- fault
クライアントまたはサービス (双方向の場合) に返される System.ServiceModel.Channels.Message オブジェクト。
ProvideFault メソッドを実装して、クライアントに返されるカスタムのエラー メッセージを作成します。すべての ProvideFault 実装が呼び出されたときに、エラー メッセージがクライアントに返されます (fault が null 参照 (Visual Basic では Nothing) 以外の場合)。クライアントに例外情報が返されないようにするには、fault パラメータに null 参照 (Visual Basic では Nothing) を設定します。
次のコード例では、サービス メソッドがマネージ例外をスローしたときに、GreetingFault 型の FaultException だけを返す IErrorHandler を実装するサービスを示します。
#region IErrorHandler Members
public bool HandleError(Exception error)
{
Console.WriteLine("HandleError called.");
// Returning true indicates you performed your behavior.
return true;
}
// This is a trivial implementation that converts Exception to FaultException<GreetingFault>.
public void ProvideFault(
Exception error,
MessageVersion ver,
ref Message msg
)
{
Console.WriteLine("ProvideFault called. Converting Exception to GreetingFault....");
FaultException<GreetingFault> fe
= new FaultException<GreetingFault>(new GreetingFault(error.Message));
MessageFault fault = fe.CreateMessageFault();
msg = Message.CreateMessage(
ver,
fault,
"http://microsoft.wcf.documentation/ISampleService/SampleMethodGreetingFaultFault"
);
}
#endregion
次のコード例では、サービス動作を使用して、IErrorHandler の実装を ErrorHandlers プロパティに追加する方法を示します。
// This behavior modifies no binding parameters.
#region IServiceBehavior Members
public void AddBindingParameters(
ServiceDescription description,
ServiceHostBase serviceHostBase,
System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
System.ServiceModel.Channels.BindingParameterCollection parameters
)
{
return;
}
// This behavior is an IErrorHandler implementation and
// must be applied to each ChannelDispatcher.
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.");
foreach(ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
{
chanDisp.ErrorHandlers.Add(this);
}
}
// This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault.
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
Console.WriteLine("Validate is called.");
foreach (ServiceEndpoint se in description.Endpoints)
{
// Must not examine any metadata endpoint.
if (se.Contract.Name.Equals("IMetadataExchange")
&& se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
continue;
foreach (OperationDescription opDesc in se.Contract.Operations)
{
if (opDesc.Faults.Count == 0)
throw new InvalidOperationException(String.Format(
"EnforceGreetingFaultBehavior requires a "
+ "FaultContractAttribute(typeof(GreetingFault)) in each operation contract. "
+ "The \"{0}\" operation contains no FaultContractAttribute.",
opDesc.Name)
);
bool gfExists = false;
foreach (FaultDescription fault in opDesc.Faults)
{
if (fault.DetailType.Equals(typeof(GreetingFault)))
{
gfExists = true;
continue;
}
}
if (gfExists == false)
{
throw new InvalidOperationException(
"EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract."
);
}
}
}
}
#endregion
次のコード例では、アプリケーション構成ファイルを使用してサービス動作を読み込むようにサービスを構成する方法を示します。構成ファイル内のサービス動作を公開する方法の詳細については、「IServiceBehavior」を参照してください。
<configuration>
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.SampleService"
behaviorConfiguration="metaAndErrors">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/SampleService"/>
</baseAddresses>
</host>
<endpoint
address=""
binding="wsHttpBinding"
contract="Microsoft.WCF.Documentation.ISampleService"
/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metaAndErrors">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
<enforceGreetingFaults />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="enforceGreetingFaults"
type="Microsoft.WCF.Documentation.EnforceGreetingFaultBehavior,HostApplication"
/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
.NET Framework
サポート対象 : 3.0