Yesterday, I received an email from Ahmed Omar ElSakka asking me the following question:
“I am new to SharePoint Workflows; when creating SharePoint Custom Workflows using Visual Studio 2008, I saw many blog posts and articles saying that you should declare the properties and fields within the workflow code as public so that you can use them from the workflow activities and bind them to activity properties. I have tried to declare some properties as private or protected and yes I couldn’t find them in the dialog box when trying to bind the HistoryDescription of LogToHistoryListActivity to one of those private members, any justifications?”
My answer is:
Yes Ahmed, this is true; you cannot bind activity properties to non public members and this is attributed to the serialization mechanism used by the workflow foundation.
In other words, workflow instances stay in the memory for a relatively small time . When a workflow is waiting for an event to occur (like OnTaskChanged), the workflow runtime serializes the workflow state into XML by performing a shallow copy which means that the runtime only reads the public members when serializing the workflow instances and stores the state by calling the workflow host persistence interface. When the workflow receives a notification to get back to life (for example when the task is changed), the workflow host deserializes the XML into a new instance of the Workflow type, so if you are planning to use activities that need data, declare the workflow class members that reference this data as public”.
I hope this answers you question Ahmed.