private void RemoveExpiredInstanceContexts()
        {
            try
            {
                while (true)
                {
                    lock (SyncRoot)
                    {
                        List <SharedInstanceContextInfo> toRemove = new List <SharedInstanceContextInfo>();

                        foreach (var key in this.instanceContexts.Keys)
                        {
                            InstanceContext           context = this.instanceContexts[key];
                            SharedInstanceContextInfo info    = context.Extensions.Find <SharedInstanceContextInfo>();
                            toRemove.Add(info);
                        }

                        foreach (var info in toRemove)
                        {
                            if (info.IsExpired())
                            {
                                info.DecrementBusyCount(); // let it get to 0
                            }
                        }
                    }

                    Thread.CurrentThread.Join(1000); // check again in 1 second
                }
            }
            catch (ThreadAbortException) { }
        }
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            InstanceContext           instanceContext = (InstanceContext)correlationState;
            SharedInstanceContextInfo info            = instanceContext.Extensions.Find <SharedInstanceContextInfo>();

            info.DecrementBusyCount();
        }
        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);
        }
        public void NotifyIdle(InstanceContextIdleCallback callback, InstanceContext instanceContext)
        {
            SharedInstanceContextInfo info = instanceContext.Extensions.Find <SharedInstanceContextInfo>();

            info.SetIdleCallback(callback);
        }