OperationContextScope 构造函数

定义

初始化 OperationContextScope 类的新实例。

重载

OperationContextScope(IContextChannel)

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

OperationContextScope(OperationContext)

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

OperationContextScope(IContextChannel)

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

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)

参数

channel
IContextChannel

为新的 OperationContext 创建范围时要使用的信道。

示例

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

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

注解

使用 OperationContextScope 构造函数为客户端信道创建新的 OperationContext,此客户端信道可用于添加或修改出站消息头、读取传入消息头,或访问 OperationContext 的其他运行时属性。

添加到新创建的 OperationContext.IncomingMessageHeadersOperationContext 属性的标头仅适用于传递到 OperationContextScope 构造函数的信道。 如果用户在其范围内创建了新的信道,则该标头不适用于在新信道上发送的消息。

适用于

OperationContextScope(OperationContext)

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

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)

参数

context
OperationContext

创建的范围中的活动 OperationContext

注解

使用 OperationContextScope 构造函数创建一个代码块,其中指定的 OperationContextScope 对象为当前范围。

适用于