Langue

ServiceContractAttribute.CallbackContract Propriété

Définition

Obtient ou définit le type de contrat de rappel lorsque le contrat est un contrat duplex.

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

Valeur de propriété

Qui Type indique le contrat de rappel. La valeur par défaut est null.

Exemples

L’exemple de code suivant montre un service qui spécifie un contrat de rappel, qui indique qu’un service de type IDuplexHello doit avoir un correspondant qui implémente un service de type IHelloCallbackContract. En outre, IHelloCallbackContract implémente une méthode de rappel unidirectionnelle, permettant au service d’appeler le client sans attendre une réponse pour prendre en charge un client distribué piloté par les événements.

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);
    }
  }
}

Remarques

Spécifiez une interface dans la CallbackContract propriété qui représente le contrat opposé requis dans un échange de messages bidirectionnel (ou duplex). Cela permet aux applications clientes d’écouter les appels d’opération entrante que l’application de service côté serveur peut envoyer indépendamment de l’activité du client. Les contrats de rappel qui ont des opérations unidirectionnelles représentent les appels du service que le client peut gérer.

Note

L’attribut ServiceContractAttribute est ignoré sur les contrats de rappel. Pour configurer le comportement d’exécution des objets de rappel, utilisez le System.ServiceModel.CallbackBehaviorAttribute.

S’applique à