Freigeben über


Windows Workflow 4.0 – Workflow Instance Extensions

Update 01/20/2009: Since I failed to mention this before I wanted to make something clear about these workflow posts. All information in these posts is based on the knowledge I gained through developing a framework based on Windows Workflow 4.0. I am not a member of the team nor is this information in any way endorsed by the team.  

In the previous posts we covered one way you could implement a producer-consumer pattern in workflow. This approach used composition and attempted to hide the complexities of the underlying operations inside of the respective activities through composition. However, one scenario in particular this model does not allow you to implement involves modeling of existing .NET events in a workflow. For instance, if we want to implement a FileSystemWatcher activity we need to provide some way for consumers to hook into the events exposed by the .NET object. The only way to accomplish our goal is to hook up to the .NET events and schedule workflow actions from our event handler – but how would this be implemented? Let’s take a look at some more pieces of the workflow framework that will allow us to accomplish this goal.

Workflow Extensions

Each workflow instance has a collection of objects known as extensions that are able to interface with both the workflow and the host. By default no extensions exist, but they may be added to an instance by the host prior to calling the run method using the WorkflowInstanceExtensionManager. Both WorkflowApplication and WorkflowInvoker provide access to the extension manager by a property named Extensions. There are two distinct ways to attach an extension to a workflow instance using this property:

  • WorkflowInstanceExtensionManager.Add(Object singletonExtension) – Allows you to add a singleton instance of an object to the workflow instance. This is useful if you intend to share a particular extension across instances. It is up to the host to manage the lifetime of the service, although the workflow will remove references to your service.
  • WorkflowInstanceExtensionManager.Add<T>(Func<T> extensionCreationFunction) – Allows you to provide an anonymous function to create your extension on demand. Services created through this mechanism are owned by the workflow instance so the lifetime is managed by the instance as well. If the service object implements IDisposable then the dispose method will be invoked when the instance is unloaded.

There is a third way to add extensions to a workflow instance, and is typically used when an activity requires an extension for correct function. If you have been following through these posts, you may recall our usage of the method NativeActivity.CacheMetadata(NativeActivityMetadata) when implementing the ParallelItemScheduler<T>. In addition to providing a location to declare your arguments and variables, you may also use the method for validation warnings/errors and more importantly providing extension creation functions for required extensions.

Another related scenario for the CacheMetadata method is to inform the runtime that you require an extension that is not provided by you. The following two APIs allow you to specify extension types that you require for an activity to function correctly. If the extension does not exist at runtime a validation error is created informing the user that a required extension does not exist.

If you take a quick look back at the API for adding extensions you’ll notice that an extension is just an Object and is not restricted to any particular base class or type. However, there are some scenarios where the extension may need to interact with the workflow via bookmarks, and in order to do that we need some way to get the WorkflowInstanceProxy (a trimmed down version of WorkflowApplication that provides limited method access from inside a workflow) so we can resume bookmarks. This is where the interface IWorkflowInstanceExtension comes in handy! This interface is defined as follows:

 namespace System.Activities.Hosting
{
    public interface IWorkflowInstanceExtension
    {
        IEnumerable<Object> GetAdditionalExtensions();
        void SetInstance(WorkflowInstanceProxy instance);
    }
}

Any time an extension is attached (though either mechanism) that implements the above interface the runtime will invoke both methods. The first method provides an entry point for creating additional extensions. The second method is the more interesting one for our current discussion, as it provides the extension with the owning workflow instance proxy. A typical implementation for this method is to simply store the provided parameter into a member variable for later use by the extension. For illustration purposes, and since it leads us to our next destination, I have pasted an example of how we might design an item scheduling extension for interfacing with our ParallelItemScheduler<T> activity.

 public sealed class ItemSchedulerExtension<T> : IWorkflowInstanceExtension
{
    internal ItemSchedulerExtension()
    {
    }

    IEnumerable<Object> IWorkflowInstanceExtension.GetAdditionalExtensions()
    {
        return null;
    }

    void IWorkflowInstanceExtension.SetInstance(WorkflowInstanceProxy instance)
    {
        m_instance = instance;
    }

    public void Schedule(Bookmark bookmark, T item)
    {
        m_instance.BeginResumeBookmark(bookmark, item, s_timeout, new AsyncCallback(OnEndResumeBookmark), null);
    }

    void OnEndResumeBookmark(IAsyncResult result)
    {
        try
        {
            BookmarkResumptionResult resumptionResult = m_instance.EndResumeBookmark(result);
            Debug.Assert(resumptionResult == BookmarkResumptionResult.Success, "Error resuming bookmark. Reason: " + resumptionResult);
        }
        catch (Exception ex)
        {
            Debug.Fail("Exception occurred while resuming bookmark.", ex.ToString());
        }
    }

    private WorkflowInstanceProxy m_instance;
    static readonly TimeSpan s_timeout = TimeSpan.FromMinutes(2);
}

As you can see the implementation of this particular extension is minimal and straightforward. We do not have additional extensions so we can simply return null. Our SetInstance implementation simply stores the workflow instance proxy into our member variable so we can use it in our Schedule implementation. Finally, the schedule method simply resumes a bookmark using the asynchronous pattern for the ResumeBookmark method.

Note: I would like to point out that the synchronous versions of the bookmark methods are intentionally missing from the proxy object, since resuming a bookmark requires the workflow thread to be free. If an activity attempts to resume a bookmark on an asynchronous thread (using this extension) and the workflow thread is busy, the synchronous call blocks until the workflow thread becomes available to schedule the bookmark resumption in the queue. In order to encourage better programming practices and reduce confusion regarding which method to invoke the synchronous methods have been removed.

Conclusion

In this article we discussed workflow extensions, how the lifetime is managed, and how to attach them to a workflow instance. We also discussed how an activity can provide an extension by providing information in the CacheMetadata(*ActivityMetadata) family of methods. If you recall from the introduction I alluded to modeling a .NET event-driven object in a workflow activity. So far we have covered the first piece of the puzzle – next post I plan on revising the producer-consumer model, illustrating a different way to implement the CopyDirectory activity using this new model.

Comments

  • Anonymous
    November 25, 2010
    Anyone had EndResumeBookmark return BookmarkResumptionResult.NotReady? Tim

  • Anonymous
    June 19, 2011
    Avidly waiting for the follow up article. I have a similar scenario that I needed to address and attempting to use the producer-consumer ran into the issues you mentioned above so really looking forward to your solution. I imagine you could make it work with the producer-consumer model by having a container activity create a non-blocking, multiple resume bookmark for event listener extension to post events to. You could then have a producer sijavascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$content$ctl00$w_46719$_937625$ctl00$ctl00$ctl00$ctl05$bpCommentForm$ctl05$btnSubmit", "", true, "BlogPostCommentForm-ctl00_content_ctl00_w_46719__937625_ctl00_ctl00", "", false, true))de that simply queries the container activity (indirectly) for any posted events and returns those. While this might work it seems dirty and I'm hoping you have better solution that doesn't risk missing events while the workflow thread is tired up elsewhere.

  • Anonymous
    June 21, 2011
    Jimit, Thanks for taking the time to read through my blog posts. :-) I apologize for my lack of attention to the blog for quite some time, but I have been heads down in development for our next product release. I fully intend to resume my series on WF and introduce some new topics pertaining to WCF. Keep a look out as I plan on posting again in the near future. Patrick

  • Anonymous
    November 29, 2011
    Any word on the follow-up article for this series?

  • Anonymous
    December 01, 2011
    Once we finally wrap up development I should be able to devote more attention back to the blog. I should be able to post a follow-up for this article within the next month or two, depending on how busy we remain. Thanks for your interest and I hope you continue to read! Patrick

  • Anonymous
    August 20, 2013
    Patrick, I guess you got more busy than expected.  Did you post your series elsewhere?

  • Anonymous
    August 28, 2015
    It's likely a longshot at this point, but if there's any chance that this series could be continued, I would still be interested.  Seems like the amount of information related to workflow extensions is sparse at best.  This seems to be an awesome wealth of information.