Lingua

OperationContext.Current Proprietà

Definizione

Ottiene o imposta il contesto di esecuzione per il thread corrente.

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

Valore della proprietà

Oggetto OperationContext che rappresenta il contesto di messaggistica ed esecuzione del metodo corrente.

Esempio

Nell'esempio di codice seguente viene utilizzata la proprietà e Current il GetCallbackChannel metodo per creare un canale di nuovo al chiamante dall'interno di un metodo. Tutti i metodi in questo esempio sono metodi unidirezionale, consentendo al servizio e al client di comunicare in entrambe le direzioni in modo indipendente. In questo caso, l'applicazione client di esempio prevede una sola chiamata restituita prima che venga chiusa, ma un altro client, ad esempio un client Windows Form, può ricevere un numero qualsiasi di chiamate dal servizio.

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

Commenti

Utilizzare la Current proprietà per ottenere il contesto di esecuzione e messaggio per il metodo corrente.

Si applica a