I wanted to create a custom State that did certain things at start up and shutdown (built in, rather than use StateInitialization and StateFinalization).
Although I could create a customState inheriting from State, it seems the Initialize() method is called twice Is this a bug Uninitialize is only called once.
If not overriding Initialize/Uninitialize which should I hook into for start/end of state

Custom State ?
fbalas
OnActivityExecutionContextLoad and
OnActivityExecutionContextUnLoad
which seem to fit the bill - is this right
sobo1
You could use Initialize/OnClose or OnActivityExecutionContextLoad/ OnActivityExecutionContextUnload, both will give you similar results. As an example I created a custom state and overrode all four methods and just traced their execution, along with the Execute method. Then I added two instances of it to a workflow and set them as the Initial and Completed state, and added a StateInitialization with a SetState pointing to the completed state into the initial state. The results were the following:
OnActivityExecutionContextLoad called for 'custState1'
OnActivityExecutionContextLoad called for 'custState2'
Initialize called for 'custState1'
Initialize called for 'custState2'
OnActivityExecutionContextLoad called for 'custState1'
Initialize called for 'custState1'
Execute called for 'custState1'
OnClosed called for 'custState1'
OnActivityExecutionContextUnload called for 'custState1'
OnActivityExecutionContextLoad called for 'custState2'
Initialize called for 'custState2'
Execute called for 'custState2'
OnClosed called for 'custState2'
OnActivityExecutionContextUnload called for 'custState2'
OnActivityExecutionContextUnload called for 'custState1'
OnActivityExecutionContextUnload called for 'custState2'
As you can see OnActivityExecutionContextLoad is called before Initialize, but they are both called the same amount of times. OnClosed however is only called once per execution of the custom activity.
NathanCaaaaaaaan
NeederOfVBHelp
missle
I've found that Initialize is called when an instance of the containing workflow is created, not when the State is entered into.
Is there a different event I should be capturing (equivalent to the StateInitialization activity)
Spenceee
Many thanks for the info and continued help. I was not quite clear from that exactly how I could key into the points where the state is entered into or exits (equivalents to StateInitialization and StateFinalization). Initialize and ContextLoad seem to be called at the creation of the workflow process as well as executing multiple times.
I did my own test in the same way, and found that Execute is called just before any StateInitialization activity (which makes sense), and the OnClosed is raised just after StateFinalization is called.
I think those are the points I need to hook in my inbuilt functions. This subclassed state now supports creating a database entry for a human to follow up, and will automatically close down this entry when the state is closed.