public static Invoker Create(Guid instanceId) { // workflow runtime core WorkflowRuntime workflowRuntime = WorkflowHosting.WorkflowRuntime; // Get a reference to the ExternalDataExchangeService from the WorkflowRuntime ExternalDataExchangeService dataExchangeService = workflowRuntime.GetService <ExternalDataExchangeService>(); // Get a reference to the LocalService InvokerLocalService invokerLocalService = (InvokerLocalService)dataExchangeService.GetService(typeof(InvokerLocalService)); // get instance of the workflow WorkflowInstance instance = workflowRuntime.GetWorkflow(instanceId); try { // workaround to detect WorkflowStatus, if the status == Created, the executor will start this instance instance.Start(); } catch (Exception ex) { // WorkflowStatus != Created (therefore we can go ahead and use it) Trace.WriteLine(ex.Message); } // plug-in the local service Invoker invoker = new Invoker(invokerLocalService, instance, true); // now we can communicate with workflow instance return(invoker); }
public Invoker(InvokerLocalService localservice, WorkflowInstance instance, bool bStateMachine) { _instance = instance; _localservice = localservice; _bStateMachine = bStateMachine; // response handler _localservice.Response += new EventHandler <ResponseEventArgs>(localservice_FireResponse); // exception handlers _localservice.WorkflowRuntime.WorkflowTerminated += new EventHandler <WorkflowTerminatedEventArgs>(OnWorkflowTerminated); _localservice.WorkflowRuntime.WorkflowSuspended += new EventHandler <WorkflowSuspendedEventArgs>(OnWorkflowSuspended); // completed handler _localservice.WorkflowRuntime.WorkflowCompleted += new EventHandler <WorkflowCompletedEventArgs>(OnWorkflowCompleted); }
public static Invoker Create(Type workflowType, Dictionary <string, object> namedArgumentValues, Guid instanceId, bool bStart) { if (workflowType == null) { throw new ArgumentNullException("Invoker requires a workflowType"); } // state machine? bool isStateMachine = (workflowType.BaseType == typeof(StateMachineWorkflowActivity)); // workflow runtime core WorkflowRuntime workflowRuntime = WorkflowHosting.WorkflowRuntime; // Get a reference to the ExternalDataExchangeService from the WorkflowRuntime ExternalDataExchangeService dataExchangeService = workflowRuntime.GetService <ExternalDataExchangeService>(); // Get a reference to the LocalService InvokerLocalService invokerLocalService = (InvokerLocalService)dataExchangeService.GetService(typeof(InvokerLocalService)); // check XOML attribute object[] attributes = workflowType.GetCustomAttributes(typeof(CreateWorkflowByXOMLAttribute), true); // create workflow instance based on the configuration options WorkflowInstance instance = null; if (attributes == null || attributes.Length == 0) { // default: create instance of the workflow instance = workflowRuntime.CreateWorkflow(workflowType, namedArgumentValues, instanceId); } else { // options: direct from the file system or via a custom XomlLoader (database, filesystem, remoting, ...) CreateWorkflowByXOMLAttribute attribute = attributes[0] as CreateWorkflowByXOMLAttribute; try { // sources link string workflowDefinitionSource = attribute.WorkflowDefinitionKey.StartsWith("@") ? ConfigurationManager.AppSettings[attribute.WorkflowDefinitionKey.Substring(1)] : attribute.WorkflowDefinitionKey; string rulesSource = attribute.RulesKey.StartsWith("@") ? ConfigurationManager.AppSettings[attribute.RulesKey.Substring(1)] : attribute.RulesKey; // text readers bool bIsXomlLoader = workflowType.GetInterface(typeof(IXomlLoader).FullName) != null; if (bIsXomlLoader) { IXomlLoader loader = Activator.CreateInstance(workflowType) as IXomlLoader; using (Stream xomlStream = loader.GetWorkflowDefinition(workflowDefinitionSource, namedArgumentValues)) using (Stream rulesStream = loader.GetWorkflowRules(rulesSource, namedArgumentValues)) using (XmlTextReader xomlReader = xomlStream == null ? null : new XmlTextReader(xomlStream)) using (XmlTextReader rulesReader = rulesStream == null ? null : new XmlTextReader(rulesStream)) { instance = workflowRuntime.CreateWorkflow(xomlReader, rulesReader, null, instanceId); } } else { using (XmlTextReader xomlReader = string.IsNullOrEmpty(workflowDefinitionSource) ? null : new XmlTextReader(workflowDefinitionSource)) using (XmlTextReader rulesReader = string.IsNullOrEmpty(rulesSource) ? null : new XmlTextReader(rulesSource)) { instance = workflowRuntime.CreateWorkflow(xomlReader, rulesReader, null, instanceId); } } } catch (WorkflowValidationFailedException ex) { string errMsg = string.Format("{0} {1} TotalErrors={2}", ex.Message, ex.Errors[0].ErrorText, ex.Errors.Count); ex.GetType().BaseType.InvokeMember("_message", BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.ExactBinding | BindingFlags.FlattenHierarchy | BindingFlags.Instance, null, ex, new object[1] { errMsg }, CultureInfo.InvariantCulture); throw ex; } catch (Exception ex) { throw ex; } } // start workflow if (bStart) { instance.Start(); } // plug-in the local service Invoker invoker = new Invoker(invokerLocalService, instance, isStateMachine); // now we can communicate with workflow instance return(invoker); }
/// <summary> /// Attach WorkfowRuntime with services based on the config file /// </summary> /// <param name="configSectionName"></param> public static WorkflowRuntime Attach(string workflowRuntimeConfig, string localServicesConfig) { ExternalDataExchangeService dataExchangeService = null; WorkflowRuntime workflowRuntime = AppDomain.CurrentDomain.GetData(WorkflowRuntimeName) as WorkflowRuntime; if (workflowRuntime == null) { lock (AppDomain.CurrentDomain.FriendlyName) { if (workflowRuntime == null) { if (workflowRuntimeConfig == null) { // default services workflowRuntime = new WorkflowRuntime(); } else { // add services from config file workflowRuntime = new WorkflowRuntime(workflowRuntimeConfig); } if (workflowRuntime == null) { throw new NullReferenceException("The WorkflowRuntime initializing failed"); } // exchange data between the host and workflow dataExchangeService = workflowRuntime.GetService <ExternalDataExchangeService>(); if (dataExchangeService == null) { if (localServicesConfig == null) { dataExchangeService = new ExternalDataExchangeService(); } else { dataExchangeService = new ExternalDataExchangeService(localServicesConfig); } // add service for exchange data workflowRuntime.AddService(dataExchangeService); } // generic local service based on the Request/Response exchange pattern InvokerLocalService invokerLocalService = (InvokerLocalService)dataExchangeService.GetService(typeof(InvokerLocalService)); if (invokerLocalService == null) { invokerLocalService = new InvokerLocalService(workflowRuntime); dataExchangeService.AddService(invokerLocalService); } // Start all services registered in this container workflowRuntime.StartRuntime(); AppDomain.CurrentDomain.SetData(WorkflowRuntimeName, workflowRuntime); } } } return(workflowRuntime); }