OperationContext.Current Proprietà

Definizione

Consente di ottenere o impostare il contesto di esecuzione del thread corrente.

C#
public static System.ServiceModel.OperationContext Current { get; set; }

Valore della proprietà

OperationContext

Oggetto OperationContext che rappresenta il contesto di esecuzione e dei messaggi del metodo corrente.

Esempio

Nell'esempio di codice seguente viene utilizzata la proprietà e GetCallbackChannel il Current metodo per creare un canale di nuovo al chiamante dall'interno di un metodo. Tutti i metodi descritti in questo esempio sono metodi unidirezionali che consentono al servizio e al clienti di comunicare in modo indipendente in entrambe le direzioni. 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 Forms, può ricevere un numero qualsiasi di chiamate dal servizio.

C#
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

Prodotto Versioni
.NET Core 1.0, Core 1.1
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
UWP 10.0