Share via


OperationContext Sınıf

Tanım

Bir hizmet yönteminin yürütme bağlamı için erişim sağlar.

public ref class OperationContext sealed : System::ServiceModel::IExtensibleObject<System::ServiceModel::OperationContext ^>
public sealed class OperationContext : System.ServiceModel.IExtensibleObject<System.ServiceModel.OperationContext>
type OperationContext = class
    interface IExtensibleObject<OperationContext>
Public NotInheritable Class OperationContext
Implements IExtensibleObject(Of OperationContext)
Devralma
OperationContext
Uygulamalar

Örnekler

Aşağıdaki kod örneği, bir yöntemin Current içinden kanalı çağırana geri almak için özelliğini ve GetCallbackChannel yöntemini kullanır. Bu örnekteki tüm yöntemler tek yönlü yöntemlerdir ve hizmetin ve istemcinin her iki yönde de bağımsız olarak iletişim kurmasını sağlar. Bu durumda, örnek istemci uygulaması çıkmadan önce yalnızca bir dönüş çağrısı bekler, ancak başka bir istemci, örneğin bir Windows Forms istemcisi hizmetten herhangi bir sayıda çağrı alabilir.

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


Imports System.Collections.Generic
Imports System.ServiceModel
Imports System.Threading

Namespace Microsoft.WCF.Documentation
    <ServiceContract(Name:="SampleDuplexHello", Namespace:="http://microsoft.wcf.documentation", _
                     CallbackContract:=GetType(IHelloCallbackContract), SessionMode:=SessionMode.Required)> _
  Public Interface IDuplexHello
        <OperationContract(IsOneWay:=True)> _
        Sub Hello(ByVal greeting As String)
    End Interface

  Public Interface IHelloCallbackContract
    <OperationContract(IsOneWay := True)> _
    Sub Reply(ByVal responseToGreeting As String)
  End Interface

  Public Class DuplexHello
      Implements IDuplexHello
    Public Sub New()
      Console.WriteLine("Service object created: " & Me.GetHashCode().ToString())
    End Sub

    Protected Overrides Sub Finalize()
      Console.WriteLine("Service object destroyed: " & Me.GetHashCode().ToString())
    End Sub

    Public Sub Hello(ByVal greeting As String) Implements IDuplexHello.Hello
      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)
            Dim callerProxy = OperationContext.Current.GetCallbackChannel(Of IHelloCallbackContract)()
            Dim response = "Service object " & Me.GetHashCode().ToString() & " received: " & greeting
      Console.WriteLine("Sending back: " & response)
      callerProxy.Reply(response)
    End Sub
  End Class
End Namespace

Aşağıdaki istemci geri çağırma iletisini almak için öğesini uygular SampleDuplexHelloCallback . Önceki örnekte özelliğinin kullanılması Name nedeniyle içeri aktarılan geri çağırma sözleşmesi, hizmettekiyle aynı ad değildir. İstemcinin bir geri çağırma alıp alamayacağı veya ne zaman alabileceği konusunda hiçbir varsayımda bulunmadığını unutmayın; sunucu geri çağırması, istemcinin giden çağrısından tamamen bağımsızdır.

Not

İstemci senaryosunda sınıfını OperationContext kullanan bir örnek için bkz 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();
    }
  }
}

Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Threading

Namespace Microsoft.WCF.Documentation
  Public Class Client
      Implements SampleDuplexHelloCallback
    Private waitHandle As AutoResetEvent

    Public Sub New()
      waitHandle = New AutoResetEvent(False)
    End Sub

    Public Sub Run()
      ' Picks up configuration from the config file.
      Dim wcfClient As New SampleDuplexHelloClient(New InstanceContext(Me))
      Try
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Enter a greeting to send and press ENTER: ")
        Console.Write(">>> ")
        Console.ForegroundColor = ConsoleColor.Green
                Dim greeting = Console.ReadLine()
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Called service with: " & Constants.vbCrLf & Constants.vbTab & greeting)
        wcfClient.Hello(greeting)
        Console.WriteLine("Execution passes service call and moves to the WaitHandle.")
        Me.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 timeProblem As TimeoutException
        Console.WriteLine("The service operation timed out. " & timeProblem.Message)
        Console.ReadLine()
        wcfClient.Abort()
      Catch commProblem As CommunicationException
        Console.WriteLine("There was a communication problem. " & commProblem.Message)
        Console.ReadLine()
        wcfClient.Abort()
      End Try
    End Sub

    Public Shared Sub Main()
      Dim client As New Client()
      client.Run()
    End Sub

    Public Sub Reply(ByVal response As String) Implements SampleDuplexHelloCallback.Reply
      Console.WriteLine("Received output.")
      Console.WriteLine(Constants.vbCrLf & Constants.vbTab & response)
      Me.waitHandle.Set()
    End Sub
  End Class
End Namespace

Açıklamalar

OperationContext Geçerli işlem yürütme ortamına erişmek için bir hizmet işleminin içinden komutunu kullanın. Özellikle, işlem bağlamı çift yönlü hizmetlerdeki geri çağırma kanallarına erişmek, işlemlerin bazı bölümlerinde ek durum verilerini depolamak ve gelen ileti üst bilgilerine ve özelliklerine erişmek ve giden ileti üst bilgileri ve özellikleri eklemek için kullanılır.

Durum verilerini depolamak için uzantıları kullanma hakkında daha fazla bilgi için bkz . Genişletilebilir Nesneler.

OperationContext aşağıdaki özelliklere ve yöntemlere sahiptir.

Oluşturucular

OperationContext(IContextChannel)

bir istemci uygulamasında belirtilen IContextChannel öğesini kullanan sınıfının yeni bir örneğini OperationContext başlatır.

Özellikler

Channel

Geçerli OperationContext nesneyle ilişkili kanalı alır.

ClaimsPrincipal

İşlemle ilişkili talep tabanlı sorumluyu alır.

Current

Geçerli iş parçacığı için yürütme bağlamını alır veya ayarlar.

EndpointDispatcher

Uç noktanın incelenip değiştirileceği uç nokta dağıtıcısını alır veya ayarlar.

Extensions

Geçerli ileti bağlamından hizmet uzantıları koleksiyonunu alır.

HasSupportingTokens

Gelen iletinin destekleyici belirteçleri olup olmadığını gösteren bir değer alır.

Host

ServiceHost Geçerli hizmet nesnesinin öğesini alır.

IncomingMessageHeaders

için OperationContextgelen ileti üst bilgilerini alır.

IncomingMessageProperties

içindeki gelen iletinin ileti OperationContextözelliklerini alır.

IncomingMessageVersion

için OperationContextgelen SOAP ileti sürümünü alır.

InstanceContext

Geçerli hizmet örneğini InstanceContext yöneten nesneyi alır.

IsUserContext

Bu özellik sistem kullanımına yöneliktir ve kullanıcılar tarafından çağrılmamalıdır.

OutgoingMessageHeaders

Etkin OperationContextiçin giden ileti üst bilgilerini alır.

OutgoingMessageProperties

Etkin OperationContextiçindeki giden iletinin ileti özelliklerini alır.

RequestContext

Bu yöntem için uygulamayı alır veya ayarlar RequestContext .

ServiceSecurityContext

Bu yöntemin ServiceSecurityContext yürütüldiği yöntemi alır veya ayarlar.

SessionId

String Geçerli oturumu tanımlamak için kullanılan öğesini alır.

SupportingTokens

türünde SecurityTokenbir ICollection<T> alır.

Yöntemler

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetCallbackChannel<T>()

Geçerli işlemi çağıran istemci örneğine bir kanal alır.

GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
SetTransactionComplete()

Şu anda yürütülen işlemi yürütür.

ToString()

Geçerli nesneyi temsil eden dizeyi döndürür.

(Devralındığı yer: Object)

Ekinlikler

OperationCompleted

İşlem tamamlandığında gerçekleşir.

Şunlara uygulanır