WebOperationContext.Current
is a static property, and is available to any method, static or otherwise, as long as the method is running on that thread.
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();
}
This will result in the following outputs:
GetPlayerStatus: WebOperationContext is not null
CheckWebOperationContext: WebOperationContext is not null
CheckWebOperationContext: WebOperationContext is null
The first call to CheckWebOperationContext
is on the same thread, so the context is available to it.
The second call is on a different thread, so the context is not available.