Search code examples
workflow-foundation-4

InvalidCastException in OutArgument<T>.Set()


Strange exception, this was working fine before.

System.InvalidCastException: Cannot convert object 'Waiting' to type 'System.Activities.Statements.Pick+PickState'.
   at System.Runtime.TypeHelper.Convert[T](Object source)
   at System.Activities.Location`1.set_ValueCore(Object value)
   at System.Activities.ActivityContext.SetValueCore[T](LocationReference locationReference, T value)
   at System.Activities.ActivityContext.SetValue[T](OutArgument`1 argument, T value)
   at System.Activities.OutArgument`1.Set(ActivityContext context, T value)
   at MyApplication.WaitForStatusChange.OnBookmarkResumed(NativeActivityContext context, Bookmark bookmark, Object value)
   at System.Activities.Runtime.BookmarkCallbackWrapper.Invoke(NativeActivityContext context, Bookmark bookmark, Object value)
   at System.Activities.Runtime.BookmarkWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

MyApplication.WaitForStatusChange is a custom NativeActivity, the error occurs when the workflow is resumed on the bookmark for this activity, whith an enum for bookmar argument.

The WaitForStatusChange activity is placed inside a Pick activity (with another NativeActivity on the other branch)

screeshot

Activity code

  public class WaitForPartnerIntegrationStatusChange : NativeActivity
  {
    public OutArgument<PartnerSoftwareIntegrationStatus> Status { get; set; }

    protected override void Execute(NativeActivityContext context)
    {
      context.CreateBookmark(DocumentStatusChangeWatcher.DocumentPartnerSoftwareIntegrationStatusChangedBookmark, OnBookmarkResumed);
    }


    private void OnBookmarkResumed(NativeActivityContext context, Bookmark bookmark, object value)
    {
      if (value is PartnerSoftwareIntegrationStatus)
      {
        Status.Set(context, (PartnerSoftwareIntegrationStatus)value);
      }
    }

    protected override bool CanInduceIdle
    {
      get { return true; }
    }
  }

Solution

  • You see these kinds of strange exceptions often when you make a change to a workflow definition and try to resume a persisted workflow.

    Basically you can't make any changes to running workflows.