OperationContextScope 类

定义

创建一个块,其中 OperationContext 对象在范围之内。

public ref class OperationContextScope sealed : IDisposable
public sealed class OperationContextScope : IDisposable
type OperationContextScope = class
    interface IDisposable
Public NotInheritable Class OperationContextScope
Implements IDisposable
继承
OperationContextScope
实现

示例

下面的示例演示如何使用 OperationContextScope 在客户端应用程序中创建新的上下文以向传出消息添加自定义标头。

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;

public class Client : ISampleServiceCallback
{
  ManualResetEvent wait = null;

  Client()
  {
    this.wait = new ManualResetEvent(false);
  }

  public static void Main()
  {
    Client client = new Client();
    client.Run();
  }

  void Run()
  {
    // Picks up configuration from the config file.
    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();
    }
  }


  #region ISampleServiceCallback Members

  public void PushBack(string msg)
  {
    Console.WriteLine("Service said: " + msg);
    this.WriteHeaders(OperationContext.Current.IncomingMessageHeaders);
    this.wait.Set();
  }

  void WriteHeaders(MessageHeaders headers)
  {
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("IncomingHeader:");
    Console.ForegroundColor = ConsoleColor.Blue;
    foreach (MessageHeaderInfo h in headers)
    {
      if (!h.Actor.Equals(String.Empty))
        Console.WriteLine("\t" + h.Actor);
      Console.ForegroundColor = ConsoleColor.White;
      Console.WriteLine("\t" + h.Name);
      Console.ForegroundColor = ConsoleColor.Blue;
      Console.WriteLine("\t" + h.Namespace);
      Console.WriteLine("\t" + h.Relay);
      if (h.IsReferenceParameter == true)
      {
        Console.WriteLine("IsReferenceParameter header detected: " + h.ToString());
      }
    }
    Console.ResetColor();
  }
  #endregion
}
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Threading

Public Class Client
    Implements ISampleServiceCallback
  Private wait As ManualResetEvent = Nothing

  Private Sub New()
    Me.wait = New ManualResetEvent(False)
  End Sub

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

  Private Sub Run()
    ' Picks up configuration from the config file.
    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
  End Sub


  #Region "ISampleServiceCallback Members"

  Public Sub PushBack(ByVal msg As String) Implements ISampleServiceCallback.PushBack
    Console.WriteLine("Service said: " & msg)
    Me.WriteHeaders(OperationContext.Current.IncomingMessageHeaders)
    Me.wait.Set()
  End Sub

  Private Sub WriteHeaders(ByVal headers As MessageHeaders)
    Console.ForegroundColor = ConsoleColor.Red
    Console.WriteLine("IncomingHeader:")
    Console.ForegroundColor = ConsoleColor.Blue
    For Each h As MessageHeaderInfo In headers
      If Not h.Actor.Equals(String.Empty) Then
        Console.WriteLine(vbTab & h.Actor)
      End If
      Console.ForegroundColor = ConsoleColor.White
      Console.WriteLine(vbTab & h.Name)
      Console.ForegroundColor = ConsoleColor.Blue
      Console.WriteLine(vbTab & h.Namespace)
      Console.WriteLine(vbTab & h.Relay)
      If h.IsReferenceParameter = True Then
        Console.WriteLine("IsReferenceParameter header detected: " & h.ToString())
      End If
    Next h
    Console.ResetColor()
  End Sub
  #End Region

End Class

注解

通过使用指定的 OperationContextScope 对象,使用 OperationContext 类创建特定 OperationContext 对象的范围或新的 IContextChannel 对象的范围。 OperationContextScope可以在 Windows Communication Foundation (WCF) 服务或 WCF 客户端应用程序中使用。

OperationContextScope 对象建立了当前操作上下文之后,可以使用 OperationContext 执行以下操作:

  • 访问和修改传入和传出消息头和其他属性。

  • 访问运行库,包括调度程序、主机、信道和扩展。

  • 访问其他类型的上下文,如安全、实例和请求上下文。

  • 访问与 OperationContext 对象关联的信道,或(如果信道实现 System.ServiceModel.Channels.ISession)访问关联信道的会话标识符。

创建了 OperationContextScope 后,将存储当前的 OperationContext,并且新的 OperationContextCurrent 属性所返回。 释放 OperationContextScope 后,将还原原始 OperationContext

警告

请勿在 OperationContextScope 块中使用异步“await”模式。 当延续发生时,它可能会在其他线程上运行,而 OperationContextScope 特定于线程。 如果需要为异步调用调用调用“await”,请在 OperationContextScope 块外部使用它。

构造函数

OperationContextScope(IContextChannel)

初始化 OperationContextScope 类的新实例,该类使用指定的 IContextChannel 创建新的 OperationContext 的范围。

OperationContextScope(OperationContext)

初始化 OperationContextScope 类的新实例,以为指定的 OperationContext 对象创建范围。

方法

Dispose()

将原始 OperationContext 还原为活动上下文并回收 OperationContextScope 对象。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于