public T GetRequestObject(CreateRequestObjectHandler <T> createRequestObject, out Boolean bNew)
        {
            GCCollection();

            RequestObjectExtension requestExtention = null;

            bNew = false;

            if (OperationContext.Current != null)
            {
                requestExtention = OperationContext.Current.Extensions.Find <RequestObjectExtension>();
                if (createRequestObject != null && requestExtention == null)
                {
                    bNew             = true;
                    requestExtention = new RequestObjectExtension(CryptoServices.RandomIdToUInt32());
                    requestExtention.RequestObject = CreateAndAttachRequestObject(createRequestObject, requestExtention.Key);

                    OperationContext.Current.Extensions.Add(requestExtention);
                }
            }
            else // we are bound to thread affinity
            {
                requestExtention = ThreadRequestionExtension;

                if (createRequestObject != null && (requestExtention == null || (Thread.CurrentThread.IsThreadPoolThread && CallContext.GetData(ThreadDataName) == null)))
                {
                    if (requestExtention != null)
                    {
                        requestExtention.Dispose();
                        ThreadRequestionExtension = null;
                    }


                    var threadId = (UInt32)Thread.CurrentThread.ManagedThreadId + ReflectInsightService.SessionId;

                    if (Thread.CurrentThread.IsThreadPoolThread)
                    {
                        CallContext.SetData(ThreadDataName, new Object());
                    }

                    bNew             = true;
                    requestExtention = new RequestObjectExtension(threadId);
                    requestExtention.RequestObject = CreateAndAttachRequestObject(createRequestObject, requestExtention.Key);

                    // bind the request to the thread
                    ThreadRequestionExtension = requestExtention;
                }
            }

            return(requestExtention != null ? requestExtention.RequestObject : default(T));
        }
 public void RemoveRequest()
 {
     if (OperationContext.Current != null)
     {
         RequestObjectExtension requestExtention = OperationContext.Current.Extensions.Find <RequestObjectExtension>();
         if (requestExtention != null)
         {
             OperationContext.Current.Extensions.Remove(requestExtention);
             requestExtention.Dispose();
         }
     }
     else // we are bound to thread affinity
     {
         if (ThreadRequestionExtension != null)
         {
             CallContext.FreeNamedDataSlot(ThreadDataName);
             ThreadRequestionExtension.Dispose();
             ThreadRequestionExtension = null;
         }
     }
 }