public void InitializeInstanceContext(InstanceContext instanceContext, Message message, IContextChannel channel)
        {
            string instanceId;
            int    headerIndex = message.Headers.FindHeader(Constants.HeaderName, Constants.HeaderNamespace);

            if (headerIndex >= 0)
            {
                instanceId = message.Headers.GetHeader <string>(headerIndex);
            }
            else
            {
                instanceId = Guid.NewGuid().ToString();
            }

            SharedInstanceContextInfo info = new SharedInstanceContextInfo(instanceContext);

            info.IncrementBusyCount(); // one for the current caller
            info.IncrementBusyCount(); // one for the expiration timer

            instanceContext.Extensions.Add(info);

            lock (SyncRoot)
            {
                this.instanceContexts.Add(instanceId, instanceContext);
                if (this.expirationThread == null)
                {
                    this.expirationThread = new Thread(RemoveExpiredInstanceContexts);
                    this.expirationThread.Start();
                }
            }

            instanceContext.Closing += delegate(object sender, EventArgs e)
            {
                lock (SyncRoot)
                {
                    this.instanceContexts.Remove(instanceId);
                    if (this.instanceContexts.Count == 0)
                    {
                        this.expirationThread.Abort();
                        this.expirationThread = null;
                    }
                }
            };
        }
        public InstanceContext GetExistingInstanceContext(Message message, IContextChannel channel)
        {
            int headerIndex = message.Headers.FindHeader(Constants.HeaderName, Constants.HeaderNamespace);

            if (headerIndex >= 0)
            {
                string instanceId = message.Headers.GetHeader <string>(headerIndex);

                lock (SyncRoot)
                {
                    if (this.instanceContexts.ContainsKey(instanceId))
                    {
                        InstanceContext           context = this.instanceContexts[instanceId];
                        SharedInstanceContextInfo info    = context.Extensions.Find <SharedInstanceContextInfo>();
                        info.IncrementBusyCount();
                        return(context);
                    }
                }
            }

            return(null);
        }