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.IncomingMessageHeadersOperationContextOperationContextScope 屬性中所加入的標頭,只適用於傳遞給 建構函式的通道。 如果使用者在此範圍內建立新的通道,那麼該標頭便不會套用至這個新通道所傳送的訊息上。

適用於

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 物件是目前的範圍。

適用於