Lenguaje

ServiceContractAttribute.CallbackContract Propiedad

Definición

Obtiene o establece el tipo de contrato de devolución de llamada cuando el contrato es un contrato dúplex.

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

Valor de propiedad

que Type indica el contrato de devolución de llamada. El valor predeterminado es null.

Ejemplos

En el ejemplo de código siguiente se muestra un servicio que especifica un contrato de devolución de llamada, que indica que un servicio de tipo IDuplexHello debe tener un correspondiente que implemente un servicio de tipo IHelloCallbackContract. Además, IHelloCallbackContract implementa un método de devolución de llamada unidireccional, lo que permite al servicio llamar al cliente sin esperar a que una respuesta admita un cliente distribuido controlado por eventos.

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

Comentarios

Especifique una interfaz en la CallbackContract propiedad que representa el contrato opuesto necesario en un intercambio de mensajes bidireccional (o dúplex). Esto permite que las aplicaciones cliente escuchen las llamadas de operación entrantes que la aplicación de servicio del lado servidor puede enviar independientemente de la actividad del cliente. Los contratos de devolución de llamada que tienen operaciones unidireccionales representan llamadas del servicio que el cliente puede controlar.

Note

El ServiceContractAttribute atributo se omite en los contratos de devolución de llamada. Para configurar el comportamiento en tiempo de ejecución de los objetos de devolución de llamada, use .System.ServiceModel.CallbackBehaviorAttribute

Se aplica a