OperationContextScope Konstruktoren

Definition

Initialisiert eine neue Instanz der OperationContextScope-Klasse.

Überlädt

OperationContextScope(IContextChannel)

Initalisiert eine neue Instanz der OperationContextScope-Klasse, die mit dem angegebenen IContextChannel einen neuen OperationContext für den Bereich erstellt.

OperationContextScope(OperationContext)

Initialisiert eine neue Instanz der OperationContextScope-Klasse zur Erstellung eines Bereichs für das angegebene OperationContext-Objekt.

OperationContextScope(IContextChannel)

Initalisiert eine neue Instanz der OperationContextScope-Klasse, die mit dem angegebenen IContextChannel einen neuen OperationContext für den Bereich erstellt.

public:
 OperationContextScope(System::ServiceModel::IContextChannel ^ channel);
public OperationContextScope (System.ServiceModel.IContextChannel channel);
new System.ServiceModel.OperationContextScope : System.ServiceModel.IContextChannel -> System.ServiceModel.OperationContextScope
Public Sub New (channel As IContextChannel)

Parameter

channel
IContextChannel

Der bei der Erstellung des Bereichs für einen neuen OperationContext zu verwendende Kanal.

Beispiele

Im nachstehenden Beispiel ist dargestellt, wie mit dem OperationContextScope ein neuer Kontext in einer Clientanwendung erstellt werden kann, um der ausgehenden Nachricht einen benutzerdefinierten Header hinzuzufügen.

SampleServiceClient wcfClient = new SampleServiceClient(new InstanceContext(this));
try
{
  using (OperationContextScope scope = new OperationContextScope(wcfClient.InnerChannel))
  {
    MessageHeader header
      = MessageHeader.CreateHeader(
      "Service-Bound-CustomHeader",
      "http://Microsoft.WCF.Documentation",
      "Custom Happy Value."
      );
    OperationContext.Current.OutgoingMessageHeaders.Add(header);

    // Making calls.
    Console.WriteLine("Enter the greeting to send: ");
    string greeting = Console.ReadLine();

    //Console.ReadLine();
    header = MessageHeader.CreateHeader(
        "Service-Bound-OneWayHeader",
        "http://Microsoft.WCF.Documentation",
        "Different Happy Value."
      );
    OperationContext.Current.OutgoingMessageHeaders.Add(header);

    // One-way
    wcfClient.Push(greeting);
    this.wait.WaitOne();

    // Done with service.
    wcfClient.Close();
    Console.WriteLine("Done!");
    Console.ReadLine();
  }
}
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();
}
Dim wcfClient As New SampleServiceClient(New InstanceContext(Me))
Try
  Using scope As New OperationContextScope(wcfClient.InnerChannel)
            Dim header As MessageHeader = MessageHeader.CreateHeader("Service-Bound-CustomHeader", _
                                "http://Microsoft.WCF.Documentation", "Custom Happy Value.")
    OperationContext.Current.OutgoingMessageHeaders.Add(header)

    ' Making calls.
    Console.WriteLine("Enter the greeting to send: ")
    Dim greeting As String = Console.ReadLine()

    'Console.ReadLine();
            header = MessageHeader.CreateHeader("Service-Bound-OneWayHeader", _
                                                "http://Microsoft.WCF.Documentation", "Different Happy Value.")
    OperationContext.Current.OutgoingMessageHeaders.Add(header)

    ' One-way
    wcfClient.Push(greeting)
    Me.wait.WaitOne()

    ' Done with service. 
    wcfClient.Close()
    Console.WriteLine("Done!")
    Console.ReadLine()
  End Using
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

Hinweise

Verwenden Sie den OperationContextScope-Konstruktor zur Erstellung eines neuen OperationContext für einen Clientkanal, mit dem ausgehende Nachrichtenheader hinzugefügt oder geändert werden können, eingehende Nachrichtenheader gelesen werden können oder auf andere Laufzeiteigenschaften des OperationContext zugegriffen werden kann.

Die der OperationContext.IncomingMessageHeaders-Eigenschaft des neu erstellten OperationContext hinzugefügten Headers werden nur auf den Kanal angewendet, der an den OperationContextScope-Konstruktor übergeben wurde. Erstellt der Benutzer einen neuen Kanal innerhalb dieses Bereichs, werden die Header nicht auf Nachrichten angewendet, die an den neuen Kanal gesendet werden.

Gilt für

OperationContextScope(OperationContext)

Initialisiert eine neue Instanz der OperationContextScope-Klasse zur Erstellung eines Bereichs für das angegebene OperationContext-Objekt.

public:
 OperationContextScope(System::ServiceModel::OperationContext ^ context);
public OperationContextScope (System.ServiceModel.OperationContext context);
new System.ServiceModel.OperationContextScope : System.ServiceModel.OperationContext -> System.ServiceModel.OperationContextScope
Public Sub New (context As OperationContext)

Parameter

context
OperationContext

Der aktive OperationContext im erstellten Bereich.

Hinweise

Verwenden Sie den OperationContextScope-Konstruktor zur Erstellung eines Codeblocks, der das angegebene OperationContextScope-Objekt als aktuellen Bereich enthält.

Gilt für