OperationContractAttribute 類別

定義

表示某個方法定義了一個作業,此作業是 Windows Communication Foundation (WCF) 應用程式中服務合約的一部分。

public ref class OperationContractAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method)]
public sealed class OperationContractAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method)>]
type OperationContractAttribute = class
    inherit Attribute
Public NotInheritable Class OperationContractAttribute
Inherits Attribute
繼承
OperationContractAttribute
屬性

範例

下列程式碼範例是一個簡單的服務合約,裡面只有一個作業。

using System;
using System.Collections.Generic;
using System.Net.Security;
using System.ServiceModel;
using System.Text;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace="Microsoft.WCF.Documentation")]
  public interface ISampleService{
    // This operation specifies an explicit protection level requirement.
    [OperationContract(ProtectionLevel=ProtectionLevel.EncryptAndSign)]
    string SampleMethod(string msg);
  }

  class SampleService : ISampleService
  {
  #region ISampleService Members

  public string  SampleMethod(string msg)
  {
    Console.WriteLine("Called with: {0}", msg);
      return "The service greets you: " + msg;
  }

  #endregion
  }
}
Imports System.Net.Security
Imports System.ServiceModel
Imports System.Text

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="Microsoft.WCF.Documentation")> _
  Public Interface ISampleService
    ' This operation specifies an explicit protection level requirement.
    <OperationContract(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
    Function SampleMethod(ByVal msg As String) As String
  End Interface

  Friend Class SampleService
      Implements ISampleService
  #Region "ISampleService Members"

  Public Function SampleMethod(ByVal msg As String) As String Implements ISampleService.SampleMethod
    Console.WriteLine("Called with: {0}", msg)
       Return "The service greets you: " & msg
  End Function

  #End Region
  End Class
End Namespace

下列範例是一個實作隱含服務合約的服務,此合約指定三個作業。 其中兩個作業是雙向作業,不論其傳回值為何,它們都會傳回基礎回應訊息給呼叫者。 第三個作業接收一個呼叫 (基礎傳入訊息),但不傳回基礎回應訊息。

[ServiceContractAttribute]  
public class OneAndTwoWay  
{  
  // The client waits until a response message appears.  
  [OperationContractAttribute]  
  public int MethodOne (int x, out int y)  
  {  
    y = 34;  
    return 0;  
  }  

  // The client waits until an empty response message appears.  
  [OperationContractAttribute]  
  public void MethodTwo (int x)  
  {  
    return;  
  }  

  // The client returns as soon as an outbound message  
  // is dispatched to the service; no response  
  // message is generated or sent from the service.  
  [OperationContractAttribute(IsOneWay=true)]  
  public void MethodThree (int x)  
  {  
    return;  
  }  
}  

備註

OperationContractAttribute 套用至方法,以表示該方法實作了某個服務作業,做為服務合約的一部分 (由 ServiceContractAttribute 屬性指定)。

使用 OperationContractAttribute 屬性控制作業結構,以及中繼資料表示的值︰

  • Action 屬性指定唯一識別此作業的動作。 WCF 會根據其動作,將要求訊息分派至方法。

  • AsyncPattern 屬性表示該作業使用 Begin/End 方法組實作,或可使用 Begin/End 方法組以非同步方式呼叫該作業。

  • HasProtectionLevel 屬性表示是否已明確設定 ProtectionLevel 屬性。

  • IsOneWay 屬性表示作業僅由一個輸入訊息組成。 作業沒有相關的輸出訊息。

  • IsInitiating 屬性指定此作業是否可成為工作階段中的初始作業。

  • 屬性 IsTerminating 會指定 WCF 是否在作業完成之後嘗試終止目前的會話。

  • ProtectionLevel 屬性指定作業在執行階段所需的訊息層級安全性。

  • ReplyAction 屬性指定該作業的回覆訊息動作。

OperationContractAttribute 屬性宣告某個方法是服務合約中的作業。 只有含 OperationContractAttribute 屬性的方法才會公開成為服務作業。 若服務合約中,沒有任何方法以 OperationContractAttribute 標記,則不會公開任何作業。

屬性 AsyncPattern 表示一對 Begin<methodName和 <EndmethodName>> 方法會形成以非同步方式實作的單一作業, (用戶端或服務上) 。 服務是否能採用非同步方式實作作業,是服務實作的細節,不會公開在中繼資料上 (如 Web 服務描述語言 (WSDL))。

同樣地,用戶端可選擇以非同步方式叫用作業,不論服務方法的實作方式為何。 若服務方法的執行需要一些時間,但必須直接傳回資訊給用戶端,則建議用戶端以非同步方式呼叫服務作業。 如需詳細資訊,請參閱 AsyncPattern

IsOneWay 屬性表示某個方法不會傳回任何值,包括空的基礎回應訊息。 這種類型的方法對通知或事件型通訊很有用。 這種方法不能傳回回覆訊息,所以方法宣告必須傳回 void

重要

以程式設計方式擷取此屬性中的資訊存放區時,請使用 ContractDescription 類別,而不是反映。

注意

如果 IsOneWay 屬性設為 false (預設值),那麼就算該方法傳回 void,在基礎訊息層級一樣是雙向方法。 在此情況下,基礎結構會建立並傳送空白訊息,向呼叫者指出方法已傳回 此方式可讓應用程式與基礎結構將錯誤資訊 (例如 SOAP 錯誤) 傳回給用戶端。 將 IsOneWay 設為 true 是避免建立與分派回覆訊息的唯一方法。 如需詳細資訊,請參閱 單向服務

ActionReplyAction 屬性不只可用來修改 SOAP 訊息的預設動作,亦可建立處理常式來處理無法辨認的訊息,或停用直接訊息程式設計的加入動作。 使用 IsInitiating 屬性,避免用戶端在呼叫其他作業之前,先行呼叫某個特定服務作業。 使用 IsTerminating 屬性讓 WCF 在用戶端呼叫特定服務作業之後關閉通道。 如需詳細資訊,請參閱 使用會話

ProtectionLevel 屬性可讓您在作業合約上,指定作業訊息是否簽署、加密,或同時採用簽署與加密。 若繫結無法提供合約所需的安全性層級,在執行階段就會擲回例外狀況。 如需詳細資訊,請參閱 ProtectionLevel瞭解保護層級

建構函式

OperationContractAttribute()

初始化 OperationContractAttribute 類別的新執行個體。

屬性

Action

取得或設定要求訊息的 WS-Addressing 動作。

AsyncPattern

表示作業是使用 Begin< 服務合約中的methodName> 和 End<methodName> 方法組,以非同步方式實作。

HasProtectionLevel

取得指出此作業的訊息是否必須加密及/或簽署的值。

IsInitiating

取得或設定值,這個值表示某個方法是否實作了可以在伺服器上初始化工作階段的作業 (若這樣的工作階段存在的話)。

IsOneWay

取得或設定值,這個值會指出作業是否傳回回覆訊息。

IsTerminating

取得或設定值,這個值表示服務作業在傳送回覆訊息 (如果有的話) 之後,是否導致伺服器關閉該工作階段。

Name

取得或設定作業的名稱。

ProtectionLevel

取得或設定值,此值指定某個作業的訊息是否須加密、簽署,或兩者都進行。

ReplyAction

取得或設定作業之回覆訊息的 SOAP 動作值。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於