語言

ServiceContractAttribute.CallbackContract 屬性

定義

當合約是雙工合約時,取得或設定回呼合約的型別。

public:
 property Type ^ CallbackContract { Type ^ get(); void set(Type ^ value); };
public Type CallbackContract { get; set; }
member this.CallbackContract : Type with get, set
Public Property CallbackContract As Type

屬性值

Type A代表回電合約。 預設值為 null。

範例

以下程式碼範例展示了一個服務,該服務指定了回調合約,表示一個類型的 IDuplexHello 服務必須有一個實作型別 IHelloCallbackContract為 的服務的對應者。 此外, IHelloCallbackContract 實作單向回撥方法,使服務能在不等待回覆的情況下呼叫用戶端,以支援分散式事件驅動用戶端。

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Name = "SampleDuplexHello",
    Namespace = "http://microsoft.wcf.documentation",
    CallbackContract = typeof(IHelloCallbackContract),
    SessionMode = SessionMode.Required
  )]
  public interface IDuplexHello
  {
    [OperationContract(IsOneWay = true)]
    void Hello(string greeting);
  }

  public interface IHelloCallbackContract
  {
    [OperationContract(IsOneWay = true)]
    void Reply(string responseToGreeting);
  }

  public class DuplexHello : IDuplexHello
  {
    public DuplexHello()
    {
      Console.WriteLine("Service object created: " + this.GetHashCode().ToString());
    }

    ~DuplexHello()
    {
      Console.WriteLine("Service object destroyed: " + this.GetHashCode().ToString());
    }

    public void Hello(string greeting)
    {
      Console.WriteLine("Caller sent: " + greeting);
      Console.WriteLine("Session ID: " + OperationContext.Current.SessionId);
      Console.WriteLine("Waiting two seconds before returning call.");
      // Put a slight delay to demonstrate asynchronous behavior on client.
      Thread.Sleep(2000);
      IHelloCallbackContract callerProxy
        = OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>();
      string response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting;
      Console.WriteLine("Sending back: " + response);
      callerProxy.Reply(response);
    }
  }
}

備註

在屬性 CallbackContract 中指定一個介面,代表雙向(或雙工)訊息交換中所需的相反合約。 這使得用戶端應用程式能夠監聽伺服器端服務應用程式可獨立於用戶端活動發送的入站操作呼叫。 具有單向操作的回調合約代表客戶端可處理的服務呼叫。

Note

在回撥合約中,這個 ServiceContractAttribute 屬性會被忽略。 要設定回調物件的執行時行為,請使用 System.ServiceModel.CallbackBehaviorAttribute.

適用於