HOW TO:建立單向合約How to: Create a One-Way Contract

本主題說明的基本步驟可用來建立使用單向合約的方法。This topic shows the basic steps to create methods that use a one-way contract. 這類方法會叫用 Windows Communication Foundation (WCF) 服務,從用戶端上的作業,但不是會期待收到回覆。Such methods invoke operations on a Windows Communication Foundation (WCF) service from a client but do not expect a reply. 例如,您可以使用此合約類型,將通知發行給許多訂閱者。This type of contract can be used, for example, to publish notifications to many subscribers. 您也可以在建立雙工 (雙向) 合約時使用單向合約,以供用戶端與伺服器彼此各自進行通訊,並方便任何一方初始化對另一方的呼叫。You can also use one-way contracts when creating a duplex (two-way) contract, which allows clients and servers to communicate with each other independently so that either can initiate calls to the other. 這麼做可以特別允許伺服器對用戶端進行單向呼叫,而用戶端會將此呼叫視為事件。This can allow, in particular, the server to make one-way calls to the client that the client can treat as events. 如需指定單向方法的詳細資訊,請參閱 IsOneWay 屬性與 OperationContractAttribute 類別。For detailed information about specifying one-way methods, see the IsOneWay property and the OperationContractAttribute class.

如需建立雙工合約的用戶端應用程式的詳細資訊,請參閱How to:存取服務使用單向和要求-回覆合約For more information about creating a client application for a duplex contract, see How to: Access Services with One-Way and Request-Reply Contracts. 如需實用範例,請參閱 < 單向範例。For a working sample, see the One-Way sample.

若要建立單向合約To create a one-way contract

  1. ServiceContractAttribute 類別套用至用來定義服務要實作之方法的介面,以建立服務合約。Create the service contract by applying the ServiceContractAttribute class to the interface that defines the methods the service is to implement.

  2. 指出介面中可供用戶端叫用的方法,方法是將 OperationContractAttribute 類別套用到這些方法上。Indicate which methods in the interface a client can invoked by applying the OperationContractAttribute class to them.

  3. IsOneWay 屬性設為 true,以便將不得包含輸出的作業 (沒有傳回值,也沒有 out 或 ref 參數) 指定為單向。Designate operations that must have no output (no return value and no out or ref parameters) as one-way by setting the IsOneWay property to true. 請注意,帶有 OperationContractAttribute 類別的作業預設都會滿足要求-回覆合約,因為 IsOneWay 屬性會預設為 falseNote that the operations that carry the OperationContractAttribute class satisfy a request-reply contract by default because the IsOneWay property is false by default. 因此如果您希望方法使用單向合約,就必須明確地將屬性值指定為 trueSo you must explicitly specify the value of the attribute property to be true if you want a one-way contract for the method.

範例Example

下列程式碼範例定義包含數個單向方法的服務合約。The following code example defines a contract for a service that includes several one-way methods. 所有的方法都具有單向合約,除了 Equals 以外,因為它會預設為要求-回覆,並傳回結果。All of the methods have one-way contracts except Equals, which defaults to request-reply and returns a result.

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples", SessionMode=SessionMode.Required)]
public interface ICalculatorSession
{
    [OperationContract(IsOneWay=true)]
    void Clear();
    [OperationContract(IsOneWay = true)]
    void AddTo(double n);
    [OperationContract(IsOneWay = true)]
    void SubtractFrom(double n);
    [OperationContract(IsOneWay = true)]
    void MultiplyBy(double n);
    [OperationContract(IsOneWay = true)]
    void DivideBy(double n);
    [OperationContract]
    double Equals();
}
<ServiceContract(Namespace:="http://Microsoft.ServiceModel.Samples", SessionMode:=SessionMode.Required)> _
Public Interface ICalculatorSession

    <OperationContract(IsOneWay:=True)> _
    Sub Clear()
    <OperationContract(IsOneWay:=True)> _
    Sub AddTo(ByVal n As Double)
    <OperationContract(IsOneWay:=True)> _
    Sub SubtractFrom(ByVal n As Double)
    <OperationContract(IsOneWay:=True)> _
    Sub MultiplyBy(ByVal n As Double)
    <OperationContract(IsOneWay:=True)> _
    Sub DivideBy(ByVal n As Double)
    <OperationContract()> _
    Function Equal() As Double
End Interface

另請參閱See also