1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.ServiceModel
8 using System.ServiceModel.Channels;
9 using System.Threading;
11 public sealed class OperationContextScope : IDisposable
14 static OperationContextScope currentScope;
16 OperationContext currentContext;
18 readonly OperationContext originalContext = OperationContext.Current;
19 readonly OperationContextScope originalScope = OperationContextScope.currentScope;
20 readonly Thread thread = Thread.CurrentThread;
22 public OperationContextScope(IContextChannel channel)
24 this.PushContext(new OperationContext(channel));
27 public OperationContextScope(OperationContext context)
29 this.PushContext(context);
41 void PushContext(OperationContext context)
43 this.currentContext = context;
44 OperationContextScope.currentScope = this;
45 OperationContext.Current = this.currentContext;
50 if (this.thread != Thread.CurrentThread)
51 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidContextScopeThread0)));
53 if (OperationContextScope.currentScope != this)
54 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInterleavedContextScopes0)));
56 if (OperationContext.Current != this.currentContext)
57 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxContextModifiedInsideScope0)));
59 OperationContextScope.currentScope = this.originalScope;
60 OperationContext.Current = this.originalContext;
62 if (this.currentContext != null)
63 this.currentContext.SetClientReply(null, false);