Exemplo n.º 1
0
        protected Object[] Invoke(Type interfaceType, String methodName, bool isActivation, Object[] parameters)
        {
            Guid             workflowInstanceId = GetWorkflowInstanceId(ref isActivation);
            WorkflowInstance wfInstance;

            EventQueueName key = new EventQueueName(interfaceType, methodName);

            MethodInfo mInfo = interfaceType.GetMethod(methodName);

            bool responseRequired = (mInfo.ReturnType != typeof(void));

            if (!responseRequired)
            {
                foreach (ParameterInfo parameter in mInfo.GetParameters())
                {
                    if (parameter.ParameterType.IsByRef || parameter.IsOut)
                    {
                        responseRequired = true;
                        break;
                    }
                }
            }

            MethodMessage methodMessage = PrepareMessage(interfaceType, methodName, parameters, responseRequired);

            EventHandler <WorkflowTerminatedEventArgs> workflowTerminationHandler = null;
            EventHandler <WorkflowCompletedEventArgs>  workflowCompletedHandler   = null;

            try
            {
                if (isActivation)
                {
                    wfInstance = WorkflowRuntime.CreateWorkflow(this.workflowType, null, workflowInstanceId);
                    SafeEnqueueItem(wfInstance, key, methodMessage);
                    wfInstance.Start();
                }
                else
                {
                    wfInstance = WorkflowRuntime.GetWorkflow(workflowInstanceId);
                    SafeEnqueueItem(wfInstance, key, methodMessage);
                }

                bool workflowTerminated = false;

                //Handler for workflow termination in b/w outstanding req-response.
                workflowTerminationHandler = delegate(Object sender, WorkflowTerminatedEventArgs e)
                {
                    if (e.WorkflowInstance.InstanceId.Equals(workflowInstanceId))
                    {
                        methodMessage.SendException(e.Exception);
                        workflowTerminated = true;
                    }
                };

                workflowCompletedHandler = delegate(Object sender, WorkflowCompletedEventArgs e)
                {
                    if (e.WorkflowInstance.InstanceId.Equals(workflowInstanceId))
                    {
                        methodMessage.SendException(new ApplicationException(SR.GetString(System.Globalization.CultureInfo.CurrentCulture, SR.Error_WorkflowCompleted)));
                    }
                };

                WorkflowRuntime.WorkflowTerminated += workflowTerminationHandler;
                WorkflowRuntime.WorkflowCompleted  += workflowCompletedHandler;

                ManualWorkflowSchedulerService scheduler = WorkflowRuntime.GetService <ManualWorkflowSchedulerService>();

                if (scheduler != null)
                {
                    scheduler.RunWorkflow(wfInstance.InstanceId);
                }

                if (!responseRequired)
                {
                    // no ret, out or ref
                    return(new Object[] { });
                }

                IMethodResponseMessage response = methodMessage.WaitForResponseMessage();

                if (response.Exception != null)
                {
                    if (!workflowTerminated)
                    {
                        throw response.Exception;
                    }
                    else
                    {
                        throw new ApplicationException(SR.GetString(System.Globalization.CultureInfo.CurrentCulture, SR.Error_WorkflowTerminated), response.Exception);
                    }
                }

                if (response.OutArgs != null)
                {
                    return(((ArrayList)response.OutArgs).ToArray());
                }
                else
                {
                    return new Object[] { }
                };
            }
            finally
            {
                if (workflowTerminationHandler != null)
                {
                    WorkflowRuntime.WorkflowTerminated -= workflowTerminationHandler;
                }

                if (workflowCompletedHandler != null)
                {
                    WorkflowRuntime.WorkflowCompleted -= workflowCompletedHandler;
                }
            }
        }
        protected object[] Invoke(Type interfaceType, string methodName, bool isActivation, object[] parameters)
        {
            EventHandler <WorkflowCompletedEventArgs> handler3 = null;

            object[]       objArray;
            Guid           workflowInstanceId = GetWorkflowInstanceId(ref isActivation);
            EventQueueName key              = new EventQueueName(interfaceType, methodName);
            MethodInfo     method           = interfaceType.GetMethod(methodName);
            bool           responseRequired = method.ReturnType != typeof(void);

            if (!responseRequired)
            {
                foreach (ParameterInfo info2 in method.GetParameters())
                {
                    if (info2.ParameterType.IsByRef || info2.IsOut)
                    {
                        responseRequired = true;
                        break;
                    }
                }
            }
            MethodMessage methodMessage = PrepareMessage(interfaceType, methodName, parameters, responseRequired);
            EventHandler <WorkflowTerminatedEventArgs> handler  = null;
            EventHandler <WorkflowCompletedEventArgs>  handler2 = null;

            try
            {
                WorkflowInstance workflow;
                if (isActivation)
                {
                    workflow = this.WorkflowRuntime.CreateWorkflow(this.workflowType, null, workflowInstanceId);
                    SafeEnqueueItem(workflow, key, methodMessage);
                    workflow.Start();
                }
                else
                {
                    workflow = this.WorkflowRuntime.GetWorkflow(workflowInstanceId);
                    SafeEnqueueItem(workflow, key, methodMessage);
                }
                bool workflowTerminated = false;
                handler = delegate(object sender, WorkflowTerminatedEventArgs e) {
                    if (e.WorkflowInstance.InstanceId.Equals(workflowInstanceId))
                    {
                        methodMessage.SendException(e.Exception);
                        workflowTerminated = true;
                    }
                };
                if (handler3 == null)
                {
                    handler3 = delegate(object sender, WorkflowCompletedEventArgs e) {
                        if (e.WorkflowInstance.InstanceId.Equals(workflowInstanceId))
                        {
                            methodMessage.SendException(new ApplicationException(SR.GetString(CultureInfo.CurrentCulture, "Error_WorkflowCompleted")));
                        }
                    };
                }
                handler2 = handler3;
                this.WorkflowRuntime.WorkflowTerminated += handler;
                this.WorkflowRuntime.WorkflowCompleted  += handler2;
                ManualWorkflowSchedulerService service = this.WorkflowRuntime.GetService <ManualWorkflowSchedulerService>();
                if (service != null)
                {
                    service.RunWorkflow(workflow.InstanceId);
                }
                if (!responseRequired)
                {
                    return(new object[0]);
                }
                IMethodResponseMessage message = methodMessage.WaitForResponseMessage();
                if (message.Exception != null)
                {
                    if (!workflowTerminated)
                    {
                        throw message.Exception;
                    }
                    throw new ApplicationException(SR.GetString(CultureInfo.CurrentCulture, "Error_WorkflowTerminated"), message.Exception);
                }
                if (message.OutArgs != null)
                {
                    return(((ArrayList)message.OutArgs).ToArray());
                }
                objArray = new object[0];
            }
            finally
            {
                if (handler != null)
                {
                    this.WorkflowRuntime.WorkflowTerminated -= handler;
                }
                if (handler2 != null)
                {
                    this.WorkflowRuntime.WorkflowCompleted -= handler2;
                }
            }
            return(objArray);
        }