Langue

OperationContext.GetCallbackChannel<T> Méthode

Définition

Obtient un canal vers l’instance cliente qui a appelé l’opération actuelle.

public:
generic <typename T>
 T GetCallbackChannel();
public T GetCallbackChannel<T>();
member this.GetCallbackChannel : unit -> 'T
Public Function GetCallbackChannel(Of T) () As T

Paramètres de type

T

Type de canal utilisé pour rappeler le client.

Retours

T

Canal vers l’instance cliente qui a appelé l’opération du type spécifié dans la CallbackContract propriété.

Exemples

L’exemple de code suivant utilise la propriété et Current la GetCallbackChannel méthode pour créer un canal à partir de l’appelant à partir d’une opération. Toutes les opérations de cet exemple sont des opérations unidirectionnelles, ce qui permet au service et au client de communiquer indépendamment dans les deux sens. Dans ce cas, l’exemple d’application cliente attend un seul appel de retour avant de quitter, mais un autre client, par exemple un client Windows Forms, peut recevoir n’importe quel nombre d’appels du service.

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

Le client suivant implémente le SampleDuplexHelloCallback message de rappel pour recevoir le message de rappel. Le contrat de rappel importé n’est pas le même nom que celui du service, en raison de l’utilisation de la Name propriété dans l’exemple précédent. Notez que le client ne fait aucune hypothèse quant à la réception ou non d’un rappel ; le rappel de serveur est entièrement indépendant de l’appel sortant du client.

Note

Pour obtenir un exemple qui utilise la OperationContext classe dans un scénario client, consultez OperationContextScope.

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  public class Client : SampleDuplexHelloCallback
  {
    AutoResetEvent waitHandle;

    public Client()
    {
      waitHandle = new AutoResetEvent(false);
    }

    public void Run()
    {
      // Picks up configuration from the config file.
      SampleDuplexHelloClient wcfClient
        = new SampleDuplexHelloClient(new InstanceContext(this));
      try
      {
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Enter a greeting to send and press ENTER: ");
        Console.Write(">>> ");
        Console.ForegroundColor = ConsoleColor.Green;
        string greeting = Console.ReadLine();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Called service with: \r\n\t" + greeting);
        wcfClient.Hello(greeting);
        Console.WriteLine("Execution passes service call and moves to the WaitHandle.");
        this.waitHandle.WaitOne();
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Set was called.");
        Console.Write("Press ");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("ENTER");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write(" to exit...");
        Console.ReadLine();
        wcfClient.Close();
      }
      catch (TimeoutException timeProblem)
      {
        Console.WriteLine("The service operation timed out. " + timeProblem.Message);
        Console.ReadLine();
        wcfClient.Abort();
      }
      catch (CommunicationException commProblem)
      {
        Console.WriteLine("There was a communication problem. " + commProblem.Message);
        Console.ReadLine();
        wcfClient.Abort();
      }
    }

    public static void Main()
    {
      Client client = new Client();
      client.Run();
    }

    public void Reply(string response)
    {
      Console.WriteLine("Received output.");
      Console.WriteLine("\r\n\t" + response);
      this.waitHandle.Set();
    }
  }
}

Remarques

Appelez la GetCallbackChannel<T>() méthode pour obtenir un canal que vous pouvez utiliser pour appeler des opérations sur l’instance cliente qui a appelé le service.

S’applique à