WebOperationContext.Current
は静的プロパティであり、メソッドがそのスレッドで実行されている限り、静的またはそれ以外の任意のメソッドで使用できます。
private static void CheckWebOperationContext()
{
Trace.WriteLine(string.Format("CheckWebOperationContext: {0}", WebOperationContext.Current == null ? "WebOperationContext is null" : "WebOperationContext is not null"));
}
[OperationContract]
[WebInvoke]
public void DemonstrateWebOperationContext()
{
Trace.WriteLine(string.Format("GetPlayerStatus: {0}", WebOperationContext.Current == null ? "WebOperationContext is null" : "WebOperationContext is not null"));
CheckWebOperationContext();
// Now call the same function on a different thread
Action act = () =>
{
CheckWebOperationContext();
};
var iAsyncResult = act.BeginInvoke(null, null);
iAsyncResult.AsyncWaitHandle.WaitOne();
}
これにより、次の出力が得られます。
GetPlayerStatus: WebOperationContext が null ではありません
CheckWebOperationContext: WebOperationContext が null ではありません
CheckWebOperationContext: WebOperationContext が null です
への最初の呼び出しCheckWebOperationContext
は同じスレッド上にあるため、コンテキストを使用できます。2 番目の呼び出しは別のスレッド上にあるため、コンテキストは使用できません。