Setting custom HandleExternalEventActivity properties on activation/creation

I have what must be a common and simple requirement in my WF application but I am unable to see how it it can easily be achieved.

The application contains a large library of custom activities, many of which subclass from a single "UserActivity" activity, which in turn derives from HandleExternalEventActivity. These are all human activities, which block until a user sends a message to the runtime (hosted in WS) from the client application to notify the WF that the current activity is completed. All of this works fine.

However, during creation (not invocation) of each UserActivity for the first time I want to set some properties from internal business rules. For example, UserActivity has a DateTime property called TargetFinishTime. This is set to by adding a configured period to DateTime.Now at the time of activity creation . I am not binding property values and I do not want to set this value from the containing WF or from the previous activity (the activities are resued across many WFs). All I want is a method/event where I can set up some internal state of the HandleExternalEventActivity after it is "activated" and before it is unloaded to the persistence DB after the WF idles waiting on the external event. The Execute event is obviously unsuitable here, as is Initialize (both are sealed in HEE). Contructor doesn't work as this is used by the designer.

Am I barking up the wrong tree here or is there an easy way of doing this   Should I be writing my own IEventActivity implementation

TIA



Answer this question

Setting custom HandleExternalEventActivity properties on activation/creation

  • WII

    Can you explain what you mean by "Contructor doesn't work as this is used by the designer."

  • Vlad Shimov

    p.s. the title of this post says "Setting custom HandleExternalEventActivity properties on activation/creation" but it applies to any activity or workflow.

  • Chimme

    Hi,

    I'm still hoping someone can answer this seemingly simple question for me:

    On initial creation of a workflow instance (or activity) I need to set some custom, calculated properties. These property values are tracked, using TrackingExtracts. For the sake of simplicity, lets say the property is "StartTime" (I know this is already found in the tracking records, but bear with me). When an instance of my workflow is first created I want to set StartTime = DateTime.Now. But I can't see where to do this assignment and also get the value of StartTime tracked. I have tried the following for my custom SequentialWorkflowActivity:

    1. In an event handler for the SequentialWorkflowActivity.Initialized event.

    2. In the constructor of the custom workflow class.

    3. In overriden Execute method of my custom workflow class.

    4. In the Initialize method.

    Any value set for StartTime at these points is is not tracked when the workflow root "Executing" activity tracking record is written (although it is persisted when the WF goes idle, with the exception of setting it in the constructor). 

    The Initialize  method does allow setting a property values before the first track point, but this is not helpful for activities as this is called for all activities in a workflow when the workflow starts.

    The activity tracking record for the activity "Executing" status seems to be written before any of the above events 1 and 3 are triggered.

    Thanks


  • Wayne.C

    The (perhaps obvious) solution seems to be to use UserTrackingRecords with the appropriate TrackingExtracts rather than the standard ActivityTrackingRecords. UserTrackingRecords can be written at any time.

  • dbaf

    Sorry, that was misleading. VS.NET gave me a compile error because I tried to access the WorkflowInstanceId property in a log statement in the ctor (System.InvalidOperationException: This is an invalid design time operation. You can only perform the operation at runtime.)

    However, using the ctor of the custom Activity does not work, even after removing the log statement: the problem is that each Activity constructor is called when the workflow instance is started (it is actually called twice for each Activity, once for the definition and once for the actual activity instance, as far as I understand it). The ctor is not called when the Activity is activated, as it is deserialized from persistence store at this stage. Setting time-dependant values like TargetFinishTime makes no sense here as the constructor call does not indicate the actual "activation" of the activity.

    What I need is a lifecycle event that indicates that a HandleExternalEvent activity has become active and is now the waiting activity in the workflow.

  • Setting custom HandleExternalEventActivity properties on activation/creation