Langue

OperationContext.Current Propriété

Définition

Obtient ou définit le contexte d’exécution du thread actuel.

public:
 static property System::ServiceModel::OperationContext ^ Current { System::ServiceModel::OperationContext ^ get(); void set(System::ServiceModel::OperationContext ^ value); };
public static System.ServiceModel.OperationContext Current { get; set; }
static member Current : System.ServiceModel.OperationContext with get, set
Public Shared Property Current As OperationContext

Valeur de propriété

Qui OperationContext représente le contexte de messagerie et d’exécution de la méthode actuelle.

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 méthode. Toutes les méthodes de cet exemple sont des méthodes unidirectionnelle, 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);
    }
  }
}

Remarques

Utilisez la propriété pour obtenir le Current contexte d’exécution et de message de la méthode actuelle.

S’applique à