如何:在服务协定中声明错误
在托管代码中,出现错误条件时引发异常。 但是,在 Windows Communication Foundation (WCF) 应用程序中,通过在服务协定中声明 SOAP 错误可以使服务协定指定向客户端返回哪些错误信息。 有关异常和错误之间的关系的概述,请参阅在协定和服务中指定和处理错误。
创建指定 SOAP 错误的服务协定
创建至少包含一个操作的服务协定。 有关示例,请参阅如何:定义服务协定。
选择可以指定希望向客户端通知的错误条件的操作。 若要确定将 SOAP 错误返回到客户端的合理错误条件,请参阅在协定和服务中指定和处理错误。
为所选操作应用 System.ServiceModel.FaultContractAttribute 并向构造函数传递可序列化错误类型。 有关创建和使用可序列化类型的详细信息,请参阅在服务协定中指定数据传输。 下面的示例演示如何指定
SampleMethod
操作可以产生GreetingFault
。[OperationContract] [FaultContractAttribute( typeof(GreetingFault), Action="http://www.contoso.com/GreetingFault", ProtectionLevel=ProtectionLevel.EncryptAndSign )] string SampleMethod(string msg);
<OperationContract, FaultContractAttribute(GetType(GreetingFault), Action:="http://www.contoso.com/GreetingFault", ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _ Function SampleMethod(ByVal msg As String) As String
为向客户端传达错误条件的协定中的所有操作重复步骤 2 和步骤 3。
实现返回指定 SOAP 错误的操作
操作指定可以返回特定 SOAP 错误(例如在前面的过程中)来向调用应用程序传达错误条件后,下一步就是实现该指定。
在操作中引发指定的 SOAP 错误
当操作中出现 FaultContractAttribute 指定的错误条件时,将引发一个新的 System.ServiceModel.FaultException<TDetail>,其中指定的 SOAP 错误是类型参数。 下面的示例演示如何在前面的过程和以下代码部分中显示的
GreetingFault
中引发SampleMethod
。throw new FaultException<GreetingFault>(new GreetingFault("A Greeting error occurred. You said: " + msg));
Throw New FaultException(Of GreetingFault)(New GreetingFault("A Greeting error occurred. You said: " & msg)) End If
示例
下面的代码示例演示为 GreetingFault
操作指定 SampleMethod
的单个操作的实现。
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 rnd = new Random(DateTime.Now.Millisecond);
int test = rnd.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
}
}
Imports System.Collections.Generic
Imports System.Net.Security
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.Text
Namespace Microsoft.WCF.Documentation
<ServiceContract(Namespace:="http://microsoft.wcf.documentation")> _
Public Interface ISampleService
<OperationContract, FaultContractAttribute(GetType(GreetingFault), Action:="http://www.contoso.com/GreetingFault", ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
Function SampleMethod(ByVal msg As String) As String
End Interface
<DataContractAttribute> _
Public Class GreetingFault
Private report As String
Public Sub New(ByVal message As String)
Me.report = message
End Sub
<DataMemberAttribute> _
Public Property Message() As String
Get
Return Me.report
End Get
Set(ByVal value As String)
Me.report = value
End Set
End Property
End Class
Friend Class SampleService
Implements ISampleService
#Region "ISampleService Members"
Public Function SampleMethod(ByVal msg As String) As String Implements ISampleService.SampleMethod
Console.WriteLine("Client said: " & msg)
' Generate intermittent error behavior.
Dim rand As New Random(DateTime.Now.Millisecond)
Dim test As Integer = rand.Next(5)
If test Mod 2 <> 0 Then
Return "The service greets you: " & msg
Else
Throw New FaultException(Of GreetingFault)(New GreetingFault("A Greeting error occurred. You said: " & msg))
End If
End Function
#End Region
End Class
End Namespace