FaultContractAttribute(Type) 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 FaultContractAttribute 類別的新執行個體。
public:
FaultContractAttribute(Type ^ detailType);
public FaultContractAttribute (Type detailType);
new System.ServiceModel.FaultContractAttribute : Type -> System.ServiceModel.FaultContractAttribute
Public Sub New (detailType As Type)
參數
- detailType
- Type
包含呼叫者錯誤資訊的可序列化型別。
例外狀況
detailType
引數為 null
。
範例
下列程式碼範例將示範如何使用 FaultContractAttribute 來指定 SampleMethod
作業可以傳回 SOAP 錯誤,連同 GreetingFault
的詳細型別。
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
備註
此建構函式的典型執行方式,是將該屬性套用至服務方法,並指定一個型別當作該屬性中的引數。