views:

31

answers:

2

I have a NativeActivity derived activity that I wrote that is to use bookmarks as a trigger for a pick branch. Using something I found on MSDN I tried writing this to trigger the branch. The branch contains activities that fire service callbacks to remote clients via send activities. If I set a delay for the trigger, callbacks fire to the clients successfully. If I use my code activity, the pick branch activities don't fire.

public sealed class UpdateListener : NativeActivity<ClientUpdate>
{
    [RequiredArgument]
    public InArgument<string>     BookmarkName { get; set; }

    protected override void Execute(NativeActivityContext context)
    {
        context.CreateBookmark(BookmarkName.Get(context),
                    new BookmarkCallback(this.OnResumeBookmark));
    }


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


    public void OnResumeBookmark(NativeActivityContext context, Bookmark bookmark, object obj )
    {
        Result.Set(context, (ClientUpdate)obj);
    }
}

So it takes an arg to set the bookmark name for future bookmark references to execute the trigger. OnResumeBoookmark() takes in a ClientUpdate object that is passed by my application that is hosting the workflowapp. The activity is to return the object so the ClientUpdate can be passed to the workflow and have it sent to the remote clients via the send activity in the pick branch. In theory anyways.

For some reason it seems to be correct but feels wrong. I'm not sure if I should write the Activity in a different way to take care of what I need for my WF service.

A: 

Is there something actually resuming the bookmark here? If not the workflow will wait very patiently and nothing will happen.

Maurice
+1  A: 

I think your intentions would be a bit clearer if you created an extension (that implements IWorkflowInstanceExtension) to perform your action here.

For example:

public sealed class AsyncWorkExtension 
    : IWorkflowInstanceExtension
{
    // only one extension per workflow
    private WorkflowInstanceProxy _proxy;
    private Bookmark _lastBookmark;

    /// <summary>
    /// Request the extension does some work for an activity
    /// during which the activity will idle the workflow
    /// </summary>
    /// <param name="toResumeMe"></param>
    public void DoWork(Bookmark toResumeMe)
    {
        _lastBookmark = toResumeMe;
        // imagine I kick off some async op here
        // when complete system calls WorkCompleted below
        // NOTE:  you CANNOT block here or you block the WF!
    }

    /// <summary>
    /// Called by the system when long-running work is complete
    /// </summary>
    /// <param name="result"></param>
    internal void WorkCompleted(object result)
    {
        //NOT good practice!  example only
        //this leaks resources search APM for details
        _proxy.BeginResumeBookmark(_lastBookmark, result, null, null);
    }

    /// <summary>
    /// When implemented, returns any additional extensions 
    /// the implementing class requires.
    /// </summary>
    /// <returns>
    /// A collection of additional workflow extensions.
    /// </returns>
    IEnumerable<object> IWorkflowInstanceExtension
        .GetAdditionalExtensions()
    {
        return new object[0];
    }

    /// <summary>
    /// Sets the specified target 
    /// <see cref="WorkflowInstanceProxy"/>.
    /// </summary>
    /// <param name="instance">The target workflow instance to set.</param>
    void IWorkflowInstanceExtension
        .SetInstance(WorkflowInstanceProxy instance)
    {
        _proxy = instance;
    }
}

Within the Activity, you'd use this thusly:

 var ext = context.GetExtension<AsyncWorkExtension>();
 var bookmark = context.CreateBookmark(BookmarkCallback);
 ext.DoWork(bookmark);
 return;

This way is much more explicit (instead of using the bookmark name to convey meaning to the "outside" world) and is much easier to extend if, say, you require to send out more information than a bookmark name.

Will