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é

Type

Type qui 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, ce qui permet 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);
    }
  }
}


Imports System.Collections.Generic
Imports System.ServiceModel
Imports System.Threading

Namespace Microsoft.WCF.Documentation
    <ServiceContract(Name:="SampleDuplexHello", Namespace:="http://microsoft.wcf.documentation", _
                     CallbackContract:=GetType(IHelloCallbackContract), SessionMode:=SessionMode.Required)> _
  Public Interface IDuplexHello
        <OperationContract(IsOneWay:=True)> _
        Sub Hello(ByVal greeting As String)
    End Interface

  Public Interface IHelloCallbackContract
    <OperationContract(IsOneWay := True)> _
    Sub Reply(ByVal responseToGreeting As String)
  End Interface

  Public Class DuplexHello
      Implements IDuplexHello
    Public Sub New()
      Console.WriteLine("Service object created: " & Me.GetHashCode().ToString())
    End Sub

    Protected Overrides Sub Finalize()
      Console.WriteLine("Service object destroyed: " & Me.GetHashCode().ToString())
    End Sub

    Public Sub Hello(ByVal greeting As String) Implements IDuplexHello.Hello
      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)
            Dim callerProxy = OperationContext.Current.GetCallbackChannel(Of IHelloCallbackContract)()
            Dim response = "Service object " & Me.GetHashCode().ToString() & " received: " & greeting
      Console.WriteLine("Sending back: " & response)
      callerProxy.Reply(response)
    End Sub
  End Class
End Namespace

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 entrants que l'application de service côté serveur peut envoyer indépendamment des activités clientes. Les contrats de rappel qui ont des opérations unidirectionnelles représentent les appels du service que le client peut gérer.

Notes

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 à