Home > Programming (and Scripting) > Automatic State Transition Using the State Machine Workflow 3.5

Automatic State Transition Using the State Machine Workflow 3.5

23-Mar-10 12:35 am EST Leave a comment Go to comments
Figure 1: A small Microsoft Workflow Foundation (WF) 3.5 State Machine diagram with 2 states; the eventDrivenActivity1 contains a SetStateActivity to a second state, State_Y.
W

hen recently asked to create a State Machine Workflow (using Microsoft’s .NET Framework v3.5) to reflect an existing business process with operations in some states that wouldn’t map to the event model supported in Microsoft’s model, I posted a couple of design questions to the MSDN Forums, but didn’t get an answer (which was unusual).  What designs wouldn’t fit?  Well, one big problem was that to go from one state to another, the designer seemed built on the idea that an external event, typically a user-driven event of some sort, was responsible for executing code after a state change was made.  But in my business process, about half the state transitions were totally systemic in nature — in some cases, state transition loops could even occur that needn’t involve any external actors; neither users nor system actors.  Another big problem was that while within a given event (the main container for activities in a State Machine workflow), the exposed WCF service method could, of course, be associated easily with a ReceiveActivity.  But as soon as a SetStateActivity was processed, regardless of its placement in the event container, the Response object would be returned to the caller leaving any other logic in, say, the StateInitializationActivity of another state (being transitioned to), processed asynchronously.  This made feedback to the calling application awkward.

Okay, I thought, well there must be some way to do this.  Perhaps we need to simply have one service in the State Machine call another.  Of course, that approach failed miserably.  In the WF 3.5 platform, the State Machine workflow is itself an instance, which, once instantiated, couldn’t be accessed by making another call through WCF back to itself.  Thus usage of the SendActivity to handle transition between states wouldn’t work within a State Machine itself (though it worked beautifully when engaging other workflows, which is what it was designed for).  Then I thought of building custom activities that derived from IEventActivity which could respond to calls from within the State Machine.  But I couldn’t seem to get this working as the WorkflowQueuingService wouldn’t cooperate with my attempts to integrate the needed design.  Then it occurred to me that the easiest solution might involve leveraging the SqlWorkflowPersistenceService.

The SqlWorkflowPersistenceService in workflow is designed to facilitate tracking state over long periods of time; or at least longer than you normally want to have your workflow reside in memory, vulnerable to service outages or, more likely, other instances of it just gradually gobbling up system resources over the course of its normal period of execution.  The State Machine workflow could have a brokering class accept a method call from a client perform a given state transition using the SetStateActivity, dehydrate by means of a workflow service method and then be immediately rehydrated again afterward by another workflow service method which would continue the workflow as desired all the while holding on to the Request object until some condition was set to release it.

Figure 2: A UML sequence diagram indicating how an object leveraging the WCF service proxy can broker calls to the workflow services.  Thus, when invoking the SqlWorkflowPersistenceService, state transition and logic within the second state may be called immediately without waiting for user input or another actor to trigger an event.

Naturally, it is important that within the eventDrivenActivity1 container illustrated in Figure 1 (above) that there be at least one activity decorated with the PersistOnCloseAttribute and that the SqlWorkflowPersistenceService be appropriately configured with the state machine workflow.  Also some mechanism must be created within the object invoking the WCF Service proxy which is likewise accessible to the State Machine workflow itself, so the caller knows when the response may be returned to the client.  One option could be a static member of the workflow runtime hosting class (which can optionally override the default WorkflowServiceHostFactory class typically called in the .svc file accompanying a HTTP-based workflow service).  Another obvious option is a value stored in a database; such as one that might be responsible for aggregating and/or displaying messages from the workflow service back to a client application.

To some, the solution described in this article may seem a logical and obvious solution to the problems I outlined at the beginning, but when confronted with a new API many developers (myself among them) are unsure of what design models work in an otherwise new and unknown paradigm.  The examples I reviewed prior to writing this article were those that fit a narrow band of user-driven approaches so I thought it important to review an alternative publicly.  After all, not everyone builds their business processes according to the same philosophy and in the model I was presented with, making WF a workable solution with some guidance on how to call logic in other states would have reduced an already heavy learning curve.

Please feel free to add questions or comments or contact me by following the instructions on my blog’s main page.

Advertisements
  1. Ross
    02-Jul-10 11:12 pm EST at 11:12 pm EST

    Did my best to respond to Robin (and others) directly earlier, so if anyone else out there has questions related to this article don\’t hesistate to ask. I\’m still here, ready to answer ya! 😉

    Like

  2. Unknown
    27-Mar-10 06:00 am EST at 06:00 am EST

    Hi,i would like to have the code sample for the article you have written, i have also got the same requirement.Kindly help meIf you are ok to sedn me the code kindly let me know.Robin

    Like

  3. R. Ross
    23-Mar-10 12:58 am EST at 12:58 am EST

    I should just quickly add that the article assumes a basic understanding of Workflow Foundation 3.5, Workflow Services, Workflow Communicatin Foundation (WCF), the State Machine workflow (basic), the SQL Persistence Service and intermediate-level knowledge of the workflow service hosting model. These and other topics are covered in Pro Workflow Foundation 3.5 by APress Publishing (http://books.google.com/books?id=8xqvZrym4E0C&printsec=frontcover&dq=Pro+Workflow&as_brr=0&ei=IkqoS6bjLYO0zATtv9GICQ&cd=4#v=onepage&q=&f=false).

    Like

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Terry Glavin

CHRONICLES

Techno Manor

Geek's Corner

VM.Blog.

an IT blog.. and an occasional rant

Yammer Site Status

Is Yammer down? Offline? Broken? Undergoing scheduled maintenance? When will it be back? Find out here.

jalalaj

A journey full of wonderful experiences

Azure and beyond

My thoughts on Microsoft Azure and cloud technologies

TechCrunch

Startup and Technology News

Ottawa Citizen

Ottawa Latest News, Breaking Headlines & Sports

National Post

Canadian News, World News and Breaking Headlines

Targeted individuals's

One Government to rule them all.

Joey Li's IT Zone

Everything about IT

jenyamatya

Unravelling the magik of code...

The Bike Escape

Because Cycling is Life

The Ross Report

Now you know where you need to know more...

Lights in the Dark

A journal of space exploration

Strength Rehabilitation Institute

Bridging the gap between physiotherapy and exercise.

The Ross Report

Now you know where you need to know more...

Little Girl's Mostly Linux Blog

Nothing to see here. Move along...

David Eedle

Geek, tech, programmer, business owner. Serial starter of things. Occasional finisher. Oh, and please don't call me Dave.

%d bloggers like this: