Esempio n. 1
0
        public WorkflowApplication CreateWorkflowApp()
        {
            WorkflowApplication wfApp = new WorkflowApplication(workflow);

            wfApp.InstanceStore = this.instanceStore;

            Dictionary <XName, object> wfScope = new Dictionary <XName, object>
            {
                { WorkflowHostTypePropertyName, wfHostTypeName }
            };

            wfApp.AddInitialInstanceValues(wfScope);
            wfApp.Unloaded = (e) =>
            {
                Console.WriteLine("Unloaded");
                this.waitHandler.Set();
            };
            wfApp.Completed = (e) =>
            {
                this.completed = true;
            };
            wfApp.PersistableIdle = (e) =>
            {
                return(PersistableIdleAction.Unload);
            };
            wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs abortArgs)
            {
                Console.WriteLine("Workflow aborted (expected in this sample)");
            };
            return(wfApp);
        }
Esempio n. 2
0
        private static WorkflowApplication CreateWorkflowApplication(WorkflowHandlerBase workflowInstance, WorkflowApplicationCreationPurpose purpose, IDictionary <string, object> parameters)
        {
            try
            {
                string version;
                WorkflowApplication wfApp = null;
                var workflow = workflowInstance.CreateWorkflowInstance(out version);
                switch (purpose)
                {
                case WorkflowApplicationCreationPurpose.StartNew:
                    Dictionary <string, object> arguments = workflowInstance.CreateParameters();
                    arguments.Add(STATECONTENT, new WfContent(workflowInstance));
                    if (parameters != null)
                    {
                        foreach (var item in parameters)
                        {
                            arguments.Add(item.Key, item.Value);
                        }
                    }
                    wfApp = new WorkflowApplication(workflow, arguments);
                    workflowInstance.WorkflowDefinitionVersion = version;
                    workflowInstance.WorkflowInstanceGuid      = wfApp.Id.ToString();
                    break;

                default:
                    wfApp = new WorkflowApplication(workflow);
                    break;
                }

                WriteDebug("CreateWorkflowApplication: NodeId: " + workflowInstance.Id + ", instanceId: " + workflowInstance.WorkflowInstanceGuid + ", Purpose: " + purpose);

                InstanceHandle             ownerHandle;
                var                        store   = CreateInstanceStore(workflowInstance, out ownerHandle);
                Dictionary <XName, object> wfScope = new Dictionary <XName, object> {
                    { GetWorkflowHostTypePropertyName(), GetWorkflowHostTypeName(workflowInstance) }
                };

                wfApp.InstanceStore = store;
                wfApp.AddInitialInstanceValues(wfScope);

                wfApp.PersistableIdle      = a => { WriteDebug("PersistableIdle " + wfApp.Id); DestroyInstanceOwner(wfApp, ownerHandle); return(PersistableIdleAction.Unload); };
                wfApp.Unloaded             = b => { WriteDebug("Unloaded " + wfApp.Id); DestroyInstanceOwner(wfApp, ownerHandle); };
                wfApp.Completed            = c => { WriteDebug("Completed " + wfApp.Id); OnWorkflowCompleted(c); DestroyInstanceOwner(wfApp, ownerHandle); };
                wfApp.Aborted              = d => { WriteDebug("Aborted " + wfApp.Id); OnWorkflowAborted(d); DestroyInstanceOwner(wfApp, ownerHandle); };
                wfApp.OnUnhandledException = e => { WriteDebug("OnUnhandledException " + wfApp.Id); return(HandleError(e)); };

                wfApp.Extensions.Add(new ContentWorkflowExtension()
                {
                    WorkflowInstancePath = workflowInstance.Path
                });
                return(wfApp);
            }
            catch (Exception e)
            {
                WriteError("CreateWorkflowApplication", e);
                throw;
            }
        }
Esempio n. 3
0
        // Creates and configures a new instance of WorkflowApplication
        private static WorkflowApplication CreateWorkflowApplication(Activity rootActivity, InstanceStore store, XName wfHostTypeName)
        {
            WorkflowApplication wfApp = new WorkflowApplication(rootActivity);

            wfApp.InstanceStore = store;

            Dictionary <XName, object> wfScope = new Dictionary <XName, object>
            {
                { WorkflowHostTypePropertyName, wfHostTypeName }
            };

            // Add the WorkflowHostType value to workflow application so that it stores this data in the instance store when persisted
            wfApp.AddInitialInstanceValues(wfScope);

            // This statement is optional (see the comments in AbsoluteDelay.CacheMetadata details for more info).
            // wfApp.Extensions.Add<DurableTimerExtension>(() => new DurableTimerExtension());

            // For demonstration purposes the workflow is unloaded as soon as it is idle (and able to persist)
            wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs idleArgs)
            {
                Console.WriteLine("Workflow unloading...");
                return(PersistableIdleAction.Unload);
            };

            // Configure some tracing and synchronization for the other WorkflowApplication events

            wfApp.Unloaded = delegate(WorkflowApplicationEventArgs eargs)
            {
                if (!workflowCompleted)
                {
                    Console.WriteLine("Workflow unloaded");
                }
                else
                {
                    Console.WriteLine("Workflow unloaded after completing");
                }

                workflowUnloadedEvent.Set();
            };

            wfApp.Completed = delegate
            {
                Console.WriteLine("Workflow completed");
                workflowCompleted = true;
            };

            wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs abortArgs)
            {
                Console.WriteLine("Workflow aborted (expected in this sample)");
            };

            return(wfApp);
        }
        /// <summary>
        /// Creates the application instance with event handlers. Not using the DataEventArgument will prevent transaction id's from propagating through log4net.
        /// Relies on: PersistanceHelper.WorkflowHostTypePropertyName, PersistanceHelper.HostTypeName, PersistanceHelper.Store
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="identity"></param>
        /// <param name="extensibleArguments">Optional if reloading the workflow. Only required on inital trigger. KVP of argument name and value.</param>
        /// <remarks>DO NOT get the ID of the workflow instance before it's used otherwise on unpersisted workflows they will not work.</remarks>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        public ApplicationHelper(Activity activity, WorkflowIdentity identity, Dictionary <string, object> extensibleArguments)
        {
            if (activity == null)
            {
                throw new ArgumentNullException(nameof(activity));
            }
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            if (PersistanceHelper.Store == null)
            {
                throw new InvalidOperationException("PersistanceHelper.Store is not set. ");
            }

            if (extensibleArguments == null)
            {
                //This should only be used when the workflow is reconstitued.
                _application = new WorkflowApplication(activity, identity);
            }
            else
            {
                _application = new WorkflowApplication(
                    activity,
                    extensibleArguments,
                    identity);
            }


            //Owner of the workflow activity
            Dictionary <XName, object> wfScope = new Dictionary <XName, object>
            {
                {
                    PersistanceHelper.WorkflowHostTypePropertyName,
                    PersistanceHelper.HostTypeName
                }
            };

            _application.InstanceStore        = PersistanceHelper.Store;
            _application.OnUnhandledException = UnhandledExceptionEvent;
            _application.PersistableIdle      = AbleToPersistEvent;
            _application.Completed            = CompletedEvent;
            _application.Unloaded             = UnloadedEvent;
            _application.Aborted = AbortedEvent;
            _application.Idle    = IdleEvent;

            // Add the WorkflowHostType value to workflow application so that it stores this data in the instance store when persisted
            _application.AddInitialInstanceValues(wfScope);
        }
Esempio n. 5
0
        private static WorkflowApplication CreateWorkflowApplication(WorkflowHandlerBase workflowInstance, WorkflowApplicationCreationPurpose purpose,
                                                                     IDictionary <string, object> parameters)
        {
            string version;
            WorkflowApplication wfApp = null;
            var workflow = workflowInstance.CreateWorkflowInstance(out version);

            switch (purpose)
            {
            case WorkflowApplicationCreationPurpose.StartNew:
                Dictionary <string, object> arguments = workflowInstance.CreateParameters();
                arguments.Add(STATECONTENT, new WfContent(workflowInstance));
                if (parameters != null)
                {
                    foreach (var item in parameters)
                    {
                        arguments.Add(item.Key, item.Value);
                    }
                }
                wfApp = new WorkflowApplication(workflow, arguments);
                workflowInstance.WorkflowDefinitionVersion = version;
                workflowInstance.WorkflowInstanceGuid      = wfApp.Id.ToString();
                break;

            default:
                wfApp = new WorkflowApplication(workflow);
                break;
            }

            var store = CreateInstanceStore(workflowInstance);
            Dictionary <XName, object> wfScope = new Dictionary <XName, object>
            {
                { GetWorkflowHostTypePropertyName(), GetWorkflowHostTypeName(workflowInstance) }
            };

            wfApp.InstanceStore = store;
            wfApp.AddInitialInstanceValues(wfScope);

            wfApp.PersistableIdle      = a => { Debug.WriteLine("##WF> Pidle"); return(PersistableIdleAction.Unload); };
            wfApp.Unloaded             = b => { Debug.WriteLine("##WF> Unload"); };
            wfApp.Completed            = OnWorkflowCompleted;
            wfApp.Aborted              = OnWorkflowAborted;
            wfApp.OnUnhandledException = HandleError;

            wfApp.Extensions.Add(new ContentWorkflowExtension()
            {
                WorkflowInstancePath = workflowInstance.Path
            });
            return(wfApp);
        }
Esempio n. 6
0
        private static InstanceHandle InitializeInstanceStore(Activity workflowDefinition, WorkflowApplication wfApp)
        {
            InstanceHandle             ownerHandle;
            var                        store   = CreateInstanceStore(workflowDefinition, out ownerHandle);
            Dictionary <XName, object> wfScope = new Dictionary <XName, object> {
                { GetWorkflowHostTypePropertyName(), GetWorkflowHostTypeName(workflowDefinition) }
            };

            wfApp.InstanceStore = store;

            wfApp.AddInitialInstanceValues(wfScope);

            return(ownerHandle);
        }
Esempio n. 7
0
        public WorkflowService(
            IUsersService users

            )
        {
            _users = users;
            T      = NullLocalizer.Instance;
            Logger = NullLogger.Instance;
            SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(_users.ApplicationConnectionString);

            _wfApp = new WorkflowApplication(new NKD.Workflow.AssignMetadata());
            _wfApp.InstanceStore = store;

            XName wfHostTypeName = XName.Get("NKD", _users.ApplicationID.ToString());
            Dictionary <XName, object> wfScope = new Dictionary <XName, object> {
                { workflowHostTypePropertyName, wfHostTypeName }
            };

            _wfApp.AddInitialInstanceValues(wfScope);

            _wfApp.Extensions.Add(new MetadataExtension());
            List <XName> variantProperties = new List <XName>()
            {
                MetadataExtension.xNS.GetName("CompanyID"),
                MetadataExtension.xNS.GetName("ContactID")
            };

            store.Promote("Metadata", variantProperties, null);

            InstanceHandle handle = store.CreateInstanceHandle(null);
            var            cmd    = new CreateWorkflowOwnerCommand
            {
                InstanceOwnerMetadata =
                {
                    { workflowHostTypePropertyName, new InstanceValue(wfHostTypeName) }
                }
            };
            InstanceOwner owner = store.Execute(handle, cmd, TimeSpan.MaxValue).InstanceOwner;

            store.DefaultInstanceOwner = owner;

            handle.Free();

            _wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
            {
                return(PersistableIdleAction.Persist);
            };

            _wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    foreach (var item in e.Outputs)
                    {
                        System.Diagnostics.Debug.WriteLine("Variable:{0} has value: {1}", item.Key, item.Value);
                    }
                }
            };

            var trackingParticipant = new TrackingHelper.DebugTrackingParticipant
            {
                TrackingProfile = TrackingHelper.SimpleProfile
            };

            _wfApp.Extensions.Add(trackingParticipant);
        }