public void BeforeSendReply(ref Message reply, object correlationState)
        {
            try
            {
                if (reply != null)
                {
                    ContextMessageProperty context = null;

                    if (sessionMode == SessionMode.NotAllowed || reply.Properties.ContainsKey(suppressContextOnReply))
                    {
                        if (ContextMessageProperty.TryGet(reply, out context))
                        {
                            context.Context.Clear();
                        }
                    }
                    else
                    {
                        string newInstanceId = correlationState as string;

                        if (newInstanceId != null)
                        {
                            if (!ContextMessageProperty.TryGet(reply, out context))
                            {
                                context = new ContextMessageProperty();
                                context.Context[WellKnownContextProperties.InstanceId] = newInstanceId;
                                context.AddOrReplaceInMessage(reply);
                            }
                            else
                            {
                                context.Context[WellKnownContextProperties.InstanceId] = newInstanceId;
                            }
                        }
                    }
                }
            }
            finally
            {
                DurableInstance durableInstance = OperationContext.Current.InstanceContext.Extensions.Find <DurableInstance>();

                if (durableInstance == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              new InvalidOperationException(
                                  SR2.GetString(
                                      SR2.RequiredInstanceContextExtensionNotFound,
                                      typeof(DurableInstance).Name)));
                }
                //Decrement InstanceActivity Count
                durableInstance.DecrementActivityCount();
            }
        }
        public virtual bool IsIdle(InstanceContext instanceContext)
        {
            bool removed = false;

            if (instanceContext == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("instanceContext");
            }

            DurableInstance durableInstance = instanceContext.Extensions.Find <DurableInstance>();

            if (durableInstance == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new InvalidOperationException(
                              SR2.GetString(
                                  SR2.RequiredInstanceContextExtensionNotFound,
                                  typeof(DurableInstance).Name)));
            }

            lock (instanceContext.ThisLock)
            {
                if (instanceContext.IncomingChannels.Count == 0)
                {
                    removed = contextCache.RemoveIfNotBusy(durableInstance.InstanceId, instanceContext);
                }
            }

            if (removed && DiagnosticUtility.ShouldTraceInformation)
            {
                string traceText = SR.GetString(SR.TraceCodeDICPInstanceContextRemovedFromCache, durableInstance.InstanceId);
                TraceUtility.TraceEvent(TraceEventType.Information,
                                        TraceCode.DICPInstanceContextRemovedFromCache, SR.GetString(SR.TraceCodeDICPInstanceContextRemovedFromCache),
                                        new StringTraceRecord("InstanceDetail", traceText),
                                        this, null);
            }
            return(removed);
        }
        public virtual void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
            if (instanceContext == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("instanceContext");
            }

            if (instance == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("instance");
            }

            DurableInstance durableInstance = (DurableInstance)instance;

            if (instanceContext.State == CommunicationState.Faulted || instanceContext.Aborted)
            {
                durableInstance.Abort();
                this.durableInstanceContextProvider.UnbindAbortedInstance(instanceContext, durableInstance.InstanceId);
            }
            else if (instanceContext.State == CommunicationState.Closed)
            {
                durableInstance.Close();
            }
        }