OperationContext.Current Propiedad

Definición

Obtiene o define el contexto de ejecución del subproceso actual.

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

Valor de propiedad

OperationContext

El OperationContext que representa el contexto de mensajería y ejecución del método actual.

Ejemplos

En el ejemplo de código siguiente se usa la propiedad y GetCallbackChannel el Current método para crear un canal de vuelta al autor de la llamada desde dentro de un método . Todos los métodos en este ejemplo son unidireccionales, lo que permite que el servicio y el cliente se comuniquen en ambas direcciones de manera independiente. En este caso, la aplicación cliente de ejemplo espera sólo una llamada de devolución antes de salir, pero otro cliente; por ejemplo, un cliente de Windows Forms, puede recibir cualquier número de llamadas desde el servicio.

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

Comentarios

Utilice la Current propiedad para obtener el contexto de ejecución y mensaje para el método actual.

Se aplica a

Producto Versiones
.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