OperationContextScope 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立一個區塊,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)
{
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 物件的範圍,或使用指定的 OperationContextIContextChannel物件建立新 物件的範圍。 OperationContextScope可用於 Windows Communication Foundation (WCF) 服務或 WCF 用戶端應用程式。
一旦 OperationContextScope 物件建立了目前的作業內容,您便可使用 OperationContext 來進行下列動作︰
存取並修改傳入與傳出訊息的標頭與其他屬性。
存取執行階段,包括發送器、主機、通道與擴充部分。
存取其他型別的內容,例如安全性、執行個體與要求內容。
存取與 OperationContext 物件相關的通道,或相關通道的工作階段識別項 (若該通道實作了 System.ServiceModel.Channels.ISession)。
建立 OperationContextScope 時,會儲存目前的 OperationContext,且新的 OperationContext 會成為 Current 屬性所傳回的物件。 處置 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) |