title: dynamic load recive activity wf
body: <p>I'm trying to load and invoke activityes from custom activity as follows:</p>
  
  <p>Imagine I have a xamlx like this:</p>
  
  <p>--Sequence
   |----- LoadActiviy
   |--Initialize dictionary with input data of activitity
   |----- Invoke </p>
  
  <p>This works when activity <strong>NOT CONTAINS</strong> receive/send messages. But when i try with activity wich contains receive/send messages the result is a exception</p>
  
  <p><strong>WorkflowApplicationUnhandledExceptionEventArgs: Only registered bookmark scopes can be used for creating scoped bookmarks.</strong></p>
  
  <h2>The code:</h2>
  
  <p><strong>1-Load xaml: (Load activity)</strong></p>
  
  <pre><code>public sealed class LoadActivity : CodeActivity&lt;Activity&gt;
      {
          #region Properties
  
          /// &lt;summary&gt;
          /// Gets or sets Path.
          /// &lt;/summary&gt;
          [RequiredArgument]
          public InArgument&lt;string&gt; Path { get; set; }
  
          #endregion
  
          #region Methods
  
          /// &lt;summary&gt;
          /// The execute method.
          /// &lt;/summary&gt;
          /// &lt;param name="context"&gt;
          /// The context.
          /// &lt;/param&gt;
          /// &lt;returns&gt;
          /// An activity loaded from a file
          /// &lt;/returns&gt;
          protected override Activity Execute(CodeActivityContext context)
          {
              return ActivityXamlServices.Load(this.Path.Get(context));
          }
  
          #endregion
      }
  </code></pre>
  
  <p><strong>2- Run  activity:</strong></p>
  
  <pre><code>  public class SynchronousSynchronizationContext : SynchronizationContext
      {
          public override void Post(SendOrPostCallback d, object state)
          {
  
              d(state);
          }
      }
  
  public sealed class Invoke : CodeActivity
      {
          #region Properties
  
          /// &lt;summary&gt;
          ///   Gets or sets Activity.
          /// &lt;/summary&gt;
          /// &lt;remarks&gt;
          ///   The activity that will be invoked.  Can be loaded from XAML.
          /// &lt;/remarks&gt;
          [RequiredArgument]
          public InArgument&lt;Activity&gt; Activity { get; set; }
          public OutArgument&lt;IDictionary&lt;string, object&gt;&gt; Output { get; set; }
  
          /// &lt;summary&gt;
          ///   Gets or sets Input.
          /// &lt;/summary&gt;
          /// &lt;remarks&gt;
          ///   The input arguments you want to pass to the other workflow
          /// &lt;/remarks&gt;
          public InArgument&lt;IDictionary&lt;string, object&gt;&gt; Input { get; set; }
  
  
          #endregion
  
          // If your activity returns a value, derive from CodeActivity&lt;TResult&gt;
          // and return the value from the Execute method.
          protected override void Execute(CodeActivityContext context)
          {
              try
              {
                  IDictionary&lt;string,object&gt; _input= this.Input.Get(context);
  
                  foreach (KeyValuePair&lt;string,object&gt; 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 =&gt; Console.WriteLine("WorkflowApplication.Idle called");
  
                  Console.WriteLine("Before WorkflowApplication.Run()");
  
                  app.Run();
  
  
              }
              catch 
              {
                  throw;
              }
  
          }
      }
  </code></pre>
  
  <p>Any ideas?</p>