动态负载回收活动wf

时间:2011-10-07 06:58:24

标签: workflow-foundation-4

我正在尝试从自定义活动加载和调用活动,如下所示:

想象一下,我有一个像这样的xamlx:

- 序列  | ----- LoadActiviy  | - 使用activeitity的输入数据初始化字典  | -----调用

当活动 NOT CONTAINS 接收/发送消息时,此功能正常。但是,当我尝试使用包含接收/发送消息的活动时,结果是异常

WorkflowApplicationUnhandledExceptionEventArgs:只有注册的书签范围可用于创建范围的书签。

代码:

1-load xaml :(加载活动)

public sealed class LoadActivity : CodeActivity<Activity>
    {
        #region Properties

        /// <summary>
        /// Gets or sets Path.
        /// </summary>
        [RequiredArgument]
        public InArgument<string> Path { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// The execute method.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// An activity loaded from a file
        /// </returns>
        protected override Activity Execute(CodeActivityContext context)
        {
            return ActivityXamlServices.Load(this.Path.Get(context));
        }

        #endregion
    }

2-运行活动:

  public class SynchronousSynchronizationContext : SynchronizationContext
    {
        public override void Post(SendOrPostCallback d, object state)
        {

            d(state);
        }
    }

public sealed class Invoke : CodeActivity
    {
        #region Properties

        /// <summary>
        ///   Gets or sets Activity.
        /// </summary>
        /// <remarks>
        ///   The activity that will be invoked.  Can be loaded from XAML.
        /// </remarks>
        [RequiredArgument]
        public InArgument<Activity> Activity { get; set; }
        public OutArgument<IDictionary<string, object>> Output { get; set; }

        /// <summary>
        ///   Gets or sets Input.
        /// </summary>
        /// <remarks>
        ///   The input arguments you want to pass to the other workflow
        /// </remarks>
        public InArgument<IDictionary<string, object>> Input { get; set; }


        #endregion

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            try
            {
                IDictionary<string,object> _input= this.Input.Get(context);

                foreach (KeyValuePair<string,object> item in   _input )
                {
                    Debug.WriteLine(string.Format("{0} {1}", item.Key, item.Value));
                }

                //  AutoResetEvent idleEvent = new AutoResetEvent(false);
                WorkflowApplication app = new WorkflowApplication(this.Activity.Get(context),this.Input.Get(context));


                app.SynchronizationContext = new SynchronousSynchronizationContext();
                app.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
                {
                 //   idleEvent.Set();
                };


                app.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
                {
                    // Display the unhandled exception.
                    Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
                        e.InstanceId, e.UnhandledException.Message);

                    Console.WriteLine("ExceptionSource: {0} - {1}",
                        e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);

                    // Instruct the runtime to terminate the workflow.
                    // Other choices are Abort and Cancel. Terminate
                    // is the default if no OnUnhandledException handler
                    // is present.
                    return UnhandledExceptionAction.Terminate;
                };



                app.Idle = e => Console.WriteLine("WorkflowApplication.Idle called");

                Console.WriteLine("Before WorkflowApplication.Run()");

                app.Run();


            }
            catch 
            {
                throw;
            }

        }
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您只能在WorkflowServiceHost中托管的工作流中使用Receive活动。即使您的主工作流托管在WorkflowServiceHost中,子工作流也托管在WorkflowApplication中,并且不能包含Receive活动,因为它未作为WCF基础结构的一部分运行。