Beispiel #1
0
        public override InstanceContext GetExistingInstanceContext(Message message, IContextChannel channel)
        {
            InstanceContext instanceContext = base.GetExistingInstanceContext(message, channel);

            if (instanceContext != null && this.InstanceLifeTimeManager != null)
            {
                WorkflowDurableInstance workflowDurableInstance = instanceContext.Extensions.Find <WorkflowDurableInstance>();

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

                this.InstanceLifeTimeManager.NotifyWorkflowActivationComplete(
                    workflowDurableInstance.InstanceId,
                    this.workflowActivationCompleteCallback,
                    new WorkflowActivationCompletedCallbackState
                    (
                        workflowDurableInstance.InstanceId,
                        instanceContext),
                    false);
            }

            return(instanceContext);
        }
Beispiel #2
0
        public override bool IsIdle(InstanceContext instanceContext)
        {
            if (instanceContext == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("instanceContext");
            }

            WorkflowDurableInstance workflowDurableInstance = instanceContext.Extensions.Find <WorkflowDurableInstance>();

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

            if (this.InstanceLifeTimeManager != null)
            {
                return((!this.InstanceLifeTimeManager.IsInstanceInMemory(workflowDurableInstance.InstanceId)) &&
                       base.IsIdle(instanceContext));
            }
            return(base.IsIdle(instanceContext));
        }
        public WorkflowOperationAsyncResult(WorkflowOperationInvoker workflowOperationInvoker,
                                            WorkflowDurableInstance workflowDurableInstance, object[] inputs,
                                            AsyncCallback callback, object state, long time)
            : base(callback, state)
        {
            if (inputs == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("inputs");
            }

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

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

            string queueName;

            WorkflowRequestContext workflowRequestContext = new WorkflowRequestContext(
                this,
                inputs,
                GetContextProperties());

            queueName = workflowOperationInvoker.StaticQueueName;

            if (workflowRequestContext.ContextProperties.Count > 1) //DurableDispatchContextProperty.
            {
                queueName = QueueNameHelper.Create(workflowOperationInvoker.StaticQueueName, workflowRequestContext.ContextProperties);
            }

            WorkflowInstance workflowInstance = workflowDurableInstance.GetWorkflowInstance
                                                    (workflowOperationInvoker.CanCreateInstance);

            AsyncCallbackState callbackState = new AsyncCallbackState(workflowRequestContext,
                                                                      workflowInstance, workflowOperationInvoker.DispatchRuntime.SynchronizationContext,
                                                                      workflowOperationInvoker.InstanceLifetimeManager, queueName);

            this.isOneway       = workflowOperationInvoker.IsOneWay;
            this.instanceIdGuid = workflowInstance.InstanceId;
            this.time           = time;

            ActionItem.Schedule(waitCallback, callbackState);

            if (DiagnosticUtility.ShouldTraceVerbose)
            {
                string traceText = SR2.GetString(SR2.WorkflowOperationInvokerItemQueued, this.InstanceId, queueName);
                TraceUtility.TraceEvent(TraceEventType.Verbose,
                                        TraceCode.WorkflowOperationInvokerItemQueued, SR.GetString(SR.TraceCodeWorkflowOperationInvokerItemQueued),
                                        new StringTraceRecord("ItemDetails", traceText),
                                        this, null);
            }
        }
        public WorkflowOperationAsyncResult(WorkflowOperationInvoker workflowOperationInvoker,
            WorkflowDurableInstance workflowDurableInstance, object[] inputs,
            AsyncCallback callback, object state, long time)
            : base(callback, state)
        {
            if (inputs == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("inputs");
            }

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

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

            string queueName;

            WorkflowRequestContext workflowRequestContext = new WorkflowRequestContext(
                this,
                inputs,
                GetContextProperties());

            queueName = workflowOperationInvoker.StaticQueueName;

            if (workflowRequestContext.ContextProperties.Count > 1) //DurableDispatchContextProperty. 
            {
                queueName = QueueNameHelper.Create(workflowOperationInvoker.StaticQueueName, workflowRequestContext.ContextProperties);
            }

            WorkflowInstance workflowInstance = workflowDurableInstance.GetWorkflowInstance
                (workflowOperationInvoker.CanCreateInstance);

            AsyncCallbackState callbackState = new AsyncCallbackState(workflowRequestContext,
                workflowInstance, workflowOperationInvoker.DispatchRuntime.SynchronizationContext,
                workflowOperationInvoker.InstanceLifetimeManager, queueName);

            this.isOneway = workflowOperationInvoker.IsOneWay;
            this.instanceIdGuid = workflowInstance.InstanceId;
            this.time = time;

            ActionItem.Schedule(waitCallback, callbackState);

            if (DiagnosticUtility.ShouldTraceVerbose)
            {
                string traceText = SR2.GetString(SR2.WorkflowOperationInvokerItemQueued, this.InstanceId, queueName);
                TraceUtility.TraceEvent(TraceEventType.Verbose,
                    TraceCode.WorkflowOperationInvokerItemQueued, SR.GetString(SR.TraceCodeWorkflowOperationInvokerItemQueued),
                    new StringTraceRecord("ItemDetails", traceText),
                    this, null);
            }
        }
Beispiel #5
0
        void OnWorkflowActivationCompleted(object state)
        {
            WorkflowActivationCompletedCallbackState callbackState = (WorkflowActivationCompletedCallbackState)state;

            lock (callbackState.InstanceContext.ThisLock)
            {
                if (base.Cache.Contains(callbackState.InstanceId, callbackState.InstanceContext))
                {
                    WorkflowDurableInstance durableInstance = callbackState.InstanceContext.Extensions.Find <WorkflowDurableInstance>();
                    if (durableInstance != null &&
                        durableInstance.CurrentOperationInvocation != null &&
                        durableInstance.CurrentOperationInvocation.HasWorkflowRequestContextBeenSerialized &&
                        !durableInstance.CurrentOperationInvocation.IsCompleted)
                    {
                        // If we are here, it means the workflow instance completed, terminated, or otherwise unloaded without
                        // completing the current operation invocation. In such case, we want to make the best effort to let
                        // service model to consider this operation invocation failed.
                        try
                        {
                            durableInstance.CurrentOperationInvocation.SendFault(
                                WorkflowOperationErrorHandler.CreateUnhandledException(
                                    new InvalidOperationException(SR2.GetString(SR2.WorkflowServiceUnloadedWithoutSendingResponse))),
                                null);
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e))
                            {
                                throw;
                            }
                        }
                    }

                    IChannel[] incomingChannels = new IChannel[callbackState.InstanceContext.IncomingChannels.Count];
                    callbackState.InstanceContext.IncomingChannels.CopyTo(incomingChannels, 0);

                    if (callbackState.InstanceContext.IncomingChannels.Count != 0)
                    {
                        foreach (IChannel channel in incomingChannels)
                        {
                            callbackState.InstanceContext.IncomingChannels.Remove(channel);
                        }
                    }
                    else
                    {
                        //Call notify only when IncomingChannels Collection is empty.
                        if (callbackState.InstanceContextIdleCallback != null)
                        {
                            callbackState.InstanceContextIdleCallback(callbackState.InstanceContext);
                        }
                    }
                }
            }
        }
Beispiel #6
0
        public override void NotifyIdle(InstanceContextIdleCallback callback, InstanceContext instanceContext)
        {
            WorkflowDurableInstance workflowDurableInstance = instanceContext.Extensions.Find <WorkflowDurableInstance>();

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

            if (this.InstanceLifeTimeManager != null)
            {
                if (this.InstanceLifeTimeManager.IsInstanceInMemory(workflowDurableInstance.InstanceId))
                {
                    this.InstanceLifeTimeManager.NotifyWorkflowActivationComplete(workflowDurableInstance.InstanceId,
                                                                                  Fx.ThunkCallback(new WaitCallback(this.OnWorkflowActivationCompleted)),
                                                                                  new WorkflowActivationCompletedCallbackState
                                                                                  (
                                                                                      workflowDurableInstance.InstanceId,
                                                                                      instanceContext,
                                                                                      callback),
                                                                                  true);
                }
                else
                {
                    if (base.IsIdle(instanceContext))
                    {
                        callback(instanceContext);
                    }
                    else
                    {
                        base.NotifyIdle(callback, instanceContext);
                    }
                }
            }
            else
            {
                base.NotifyIdle(callback, instanceContext);
            }
        }
Beispiel #7
0
        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
            long beginTime = 0;

            if (PerformanceCounters.PerformanceCountersEnabled)
            {
                PerformanceCounters.MethodCalled(this.operationDescription.Name);

                try
                {
                    if (UnsafeNativeMethods.QueryPerformanceCounter(out beginTime) == 0)
                    {
                        beginTime = -1;
                    }
                }
                catch (SecurityException exception)
                {
                    DiagnosticUtility.TraceHandledException(exception, TraceEventType.Warning);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityException(SR.GetString("PartialTrustPerformanceCountersNotEnabled"), exception));
                }
            }

            Authorize();

            WorkflowDurableInstance durableInstance = (WorkflowDurableInstance)instance;

            Fx.Assert(durableInstance.CurrentOperationInvocation == null,
                      "At the time WorkflowOperationInvoker.InvokeBegin is called, the WorkflowDurableInstance.CurrentOperationInvocation is expected to be null given the ConcurrencyMode.Single.");

            durableInstance.CurrentOperationInvocation = new WorkflowOperationAsyncResult(
                this,
                durableInstance,
                inputs,
                callback,
                state,
                beginTime);

            return(durableInstance.CurrentOperationInvocation);
        }
Beispiel #8
0
        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
        {
            bool methodThrewException = false;

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

            WorkflowDurableInstance      durableInstance = (WorkflowDurableInstance)instance;
            WorkflowOperationAsyncResult asyncResult     = (WorkflowOperationAsyncResult)result;

            Fx.Assert(durableInstance.CurrentOperationInvocation != null,
                      "At the time WorkflowOperationInvoker.InvokeEnd is called, the WorkflowDurableInstance.CurrentOperationInvocation is expected to be present.");

            try
            {
                return(WorkflowOperationAsyncResult.End(asyncResult, out outputs));
            }
            catch (FaultException)
            {
                methodThrewException = true;
                if (PerformanceCounters.PerformanceCountersEnabled)
                {
                    PerformanceCounters.MethodReturnedFault(this.operationDescription.Name);
                }
                throw;
            }
            catch (Exception e)
            {
                methodThrewException = true;

                if (Fx.IsFatal(e))
                {
                    throw;
                }

                if (PerformanceCounters.PerformanceCountersEnabled)
                {
                    PerformanceCounters.MethodReturnedError(this.operationDescription.Name);
                }
                throw;
            }
            finally
            {
                durableInstance.CurrentOperationInvocation = null;

                if (!methodThrewException)
                {
                    if (PerformanceCounters.PerformanceCountersEnabled)
                    {
                        long currentTime = 0;
                        long duration    = 0;

                        if ((asyncResult.BeginTime >= 0) && (UnsafeNativeMethods.QueryPerformanceCounter(out currentTime) != 0))
                        {
                            duration = currentTime - asyncResult.BeginTime;
                        }
                        PerformanceCounters.MethodReturnedSuccess(this.operationDescription.Name, duration);
                    }
                }
            }
        }