OperationContractAttribute 類別

定義

表示某個方法定義了一個作業,此作業是 Windows Communication Foundation (WCF) 應用程式中服務合約的一部分。Indicates that a method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.

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

範例

下列程式碼範例是一個簡單的服務合約,裡面只有一個作業。The following code example shows a simple service contract with one operation.

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

下列範例是一個實作隱含服務合約的服務,此合約指定三個作業。The following example is a service that implements an implicit service contract that specifies three operations. 其中兩個作業是雙向作業,不論其傳回值為何,它們都會傳回基礎回應訊息給呼叫者。Two of the operations are two-way operations, which return underlying response messages to the caller no matter what the return value is. 第三個作業接收一個呼叫 (基礎傳入訊息),但不傳回基礎回應訊息。The third operation receives a call, an underlying inbound message, but returns no underlying response message.

[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 屬性指定)。Apply the OperationContractAttribute to a method to indicate that the method implements a service operation as part of a service contract (specified by a ServiceContractAttribute attribute).

使用 OperationContractAttribute 屬性控制作業結構,以及中繼資料表示的值︰Use the OperationContractAttribute properties to control the structure of the operation and the values expressed in metadata:

  • Action 屬性指定唯一識別此作業的動作。The Action property specifies the action that uniquely identifies this operation. WCF 會根據其動作來分派要求訊息給方法。WCF dispatches request messages to methods based on their action.

  • AsyncPattern 屬性表示該作業使用 Begin/End 方法組實作,或可使用 Begin/End 方法組以非同步方式呼叫該作業。The AsyncPattern property indicates that the operation is implemented or can be called asynchronously using a Begin/End method pair.

  • HasProtectionLevel 屬性表示是否已明確設定 ProtectionLevel 屬性。The HasProtectionLevel property indicates whether the ProtectionLevel property has been explicitly set.

  • IsOneWay 屬性表示作業僅由一個輸入訊息組成。The IsOneWay property indicates that the operation only consists of a single input message. 作業沒有相關的輸出訊息。The operation has no associated output message.

  • IsInitiating 屬性指定此作業是否可成為工作階段中的初始作業。The IsInitiating property specifies whether this operation can be the initial operation in a session.

  • IsTerminating 屬性會指定 WCF 是否會在作業完成之後,嘗試終止目前的會話。The IsTerminating property specifies whether WCF attempts to terminate the current session after the operation completes.

  • ProtectionLevel 屬性指定作業在執行階段所需的訊息層級安全性。The ProtectionLevel property specifies the message-level security that an operation requires at run time.

  • ReplyAction 屬性指定該作業的回覆訊息動作。The ReplyAction property specifies the action of the reply message for the operation.

OperationContractAttribute 屬性宣告某個方法是服務合約中的作業。The OperationContractAttribute attribute declares that a method is an operation in a service contract. 只有含 OperationContractAttribute 屬性的方法才會公開成為服務作業。Only methods attributed with the OperationContractAttribute are exposed as service operations. 若服務合約中,沒有任何方法以 OperationContractAttribute 標記,則不會公開任何作業。A service contract without any methods marked with the OperationContractAttribute exposes no operations.

AsyncPattern 屬性指出一對 Begin<方法名稱>,End<方法名稱> 方法會形成以非同步方式(不論是在用戶端或服務上)執行的單一作業。The AsyncPattern property indicates that a pair of Begin<methodName> and End<methodName> methods form a single operation implemented asynchronously (whether on the client or the service). 服務是否能採用非同步方式實作作業,是服務實作的細節,不會公開在中繼資料上 (如 Web 服務描述語言 (WSDL))。The ability of a service to implement operations asynchronously is a service implementation detail and is not exposed in metadata (such as Web Services Description Language (WSDL)).

同樣地,用戶端可選擇以非同步方式叫用作業,不論服務方法的實作方式為何。Similarly, clients can choose to invoke operations asynchronously independent of how the service method is implemented. 若服務方法的執行需要一些時間,但必須直接傳回資訊給用戶端,則建議用戶端以非同步方式呼叫服務作業。Calling service operations asynchronously in the client is recommended when a service method takes some time but must return information directly to the client. 如需詳細資訊,請參閱<AsyncPattern>。For details, see AsyncPattern.

IsOneWay 屬性表示某個方法不會傳回任何值,包括空的基礎回應訊息。The IsOneWay property indicates that a method does not return any value at all, including an empty underlying response message. 這種類型的方法對通知或事件型通訊很有用。This type of method is useful for notifications or event-style communication. 這種方法不能傳回回覆訊息,所以方法宣告必須傳回 voidMethods of this kind cannot return a reply message so the method's declaration must return void.

重要

以程式設計方式抓取此屬性中的資訊儲存區時,請使用 ContractDescription 類別,而不是反映。When programmatically retrieving the information store in this attribute, use the ContractDescription class instead of reflection.

注意

如果 IsOneWay 屬性設為 false (預設值),那麼就算該方法傳回 void,在基礎訊息層級一樣是雙向方法。If the IsOneWay property is set to false, (the default), even methods that return void are two-way methods at the underlying message level. 在這種情況下,基礎結構會建立並傳送空白訊息,向呼叫者指示該方法已經傳回。In this case, the infrastructure creates and sends an empty message to indicate to the caller that the method has returned. 此方式可讓應用程式與基礎結構將錯誤資訊 (例如 SOAP 錯誤) 傳回給用戶端。Using this approach enables the application and the infrastructure to send error information (such as a SOAP fault) back to the client. IsOneWay 設為 true 是避免建立與分派回覆訊息的唯一方法。Setting IsOneWay to true is the only way to prevent the creation and dispatch of a reply message. 如需詳細資訊,請參閱單向服務For more information, see One-Way Services.

ActionReplyAction 屬性不只可用來修改 SOAP 訊息的預設動作,亦可建立處理常式來處理無法辨認的訊息,或停用直接訊息程式設計的加入動作。The Action and ReplyAction properties can be used not only to modify the default action of SOAP messages but also to create handlers for unrecognized messages or to disable adding actions for direct message programming. 使用 IsInitiating 屬性,避免用戶端在呼叫其他作業之前,先行呼叫某個特定服務作業。Use the IsInitiating property to prevent clients from calling a particular service operation prior to other operations. 使用 [IsTerminating] 屬性,讓 WCF 在用戶端呼叫特定服務作業之後關閉通道。Use the IsTerminating property to have WCF close the channel after clients call a particular service operation. 如需詳細資訊,請參閱使用會話For more information, see Using Sessions.

ProtectionLevel 屬性可讓您在作業合約上,指定作業訊息是否簽署、加密,或同時採用簽署與加密。The ProtectionLevel property enables you to specify on the operation contract whether the operation messages are signed, encrypted, or signed and encrypted. 若繫結無法提供合約所需的安全性層級,在執行階段就會擲回例外狀況。If a binding cannot provide the security level required by the contract, an exception is thrown at run time. 如需詳細資訊,請參閱 ProtectionLevel瞭解保護層級For more information, see ProtectionLevel and Understanding Protection Level.

建構函式

OperationContractAttribute()

初始化 OperationContractAttribute 類別的新執行個體。Initializes a new instance of the OperationContractAttribute class.

屬性

Action

取得或設定要求訊息的 WS-Addressing 動作。Gets or sets the WS-Addressing action of the request message.

AsyncPattern

表示某個作業採用服務合約中的 Begin<methodName> 和 End<methodName> 方法組,以非同步方式實作。Indicates that an operation is implemented asynchronously using a Begin<methodName> and End<methodName> method pair in a service contract.

HasProtectionLevel

取得指出此作業的訊息是否必須加密及/或簽署的值。Gets a value that indicates whether the messages for this operation must be encrypted, signed, or both.

IsInitiating

取得或設定值,這個值表示某個方法是否實作了可以在伺服器上初始化工作階段的作業 (若這樣的工作階段存在的話)。Gets or sets a value that indicates whether the method implements an operation that can initiate a session on the server (if such a session exists).

IsOneWay

取得或設定值,這個值會指出作業是否傳回回覆訊息。Gets or sets a value that indicates whether an operation returns a reply message.

IsTerminating

取得或設定值,這個值表示服務作業在傳送回覆訊息 (如果有的話) 之後,是否導致伺服器關閉該工作階段。Gets or sets a value that indicates whether the service operation causes the server to close the session after the reply message, if any, is sent.

Name

取得或設定作業的名稱。Gets or sets the name of the operation.

ProtectionLevel

取得或設定值,此值指定某個作業的訊息是否須加密、簽署,或兩者都進行。Gets or sets a value that specifies whether the messages of an operation must be encrypted, signed, or both.

ReplyAction

取得或設定作業之回覆訊息的 SOAP 動作值。Gets or sets the value of the SOAP action for the reply message of the operation.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

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

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於