Windows Workflow Foundation 3
Share large data between workflow and application
Hi, there My problem is that I want to share data between the workflow and application. Here is the problem more simplified: I have created a state workflow and I have one public variable, I handle the “stateInitialization” event and I have putted here a code activity pointing to the “InitState” function. It looks like this: public sealed partial class Workflow1: StateMachineWorkflowActivity { public int myValue = 1; public Workflow1() { InitializeComponent(); } private void InitState(object sender, EventArgs e) { myValue = 3; } } My main function looks like this: static void Main(string[] args) { using(WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { AutoResetEvent waitHandle = new AutoResetEvent(false); workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();}; workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); }; WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(WorkflowMockup.Workflow1)); instance.Start(); Workflow1 aWorkflow = instance.GetWorkflowDefinition() as Workflow1; aWorkflow.myValue = 2; waitHandle.WaitOne(); } } The sequence of execution looks goes like this: myValue = 1; from the initialization. (myValue is not set) myValue = 2; from the main program. (myValue is 1 before this) And myValue = 3; from the InitState function, but myValue is still 1 and not 2. At the first appearance it seamed that there are two Workflow1 objects and they don’t have anything to do with each other. Application side is one instance and workflow side is other instance. But this is not the problem the problem might be that there are two threads and somehow the data does not get synchronized. To overcome this I found two workarounds. 1. Call external method and pass the data as a parameter. The problem here is that if you have large data structures than you need to pass it all. Need to make the data serializable and need to pass as parameter. 2. Create a Singleton class and put the data inside of this. This way you can get the data anywhere. The problem with this solution is that I personally do not like it. It is not a good design practice. Does anybody know a better way/design to solve this problem? My big picture problem is that that I want to render UI, based on Business Objects handled by workflow.
There is no direct way to share data between the workflow and the host. From the host you cannot access directly (eg myWorkflowInstance.MyProperty) Using CallExternalMethod/HandleExternalEvent with an ExternalDataExcahnge service is the way to go. If your data is large than it may be appropriate to save it to a database and then pass a key around via the communication service. If these items that you wish to pass are owned by the workflow then you could save them as UserTrackPoints and then query the TrackingDB for them in your host application. If these do not seem like they apply, please reply with additional details on your scenario and we will take a deeper look. Thanks, Steve Danielson [Microsoft]This posting is provided "AS IS" with no warranties, and confers no rights.
Use a database.
There is no direct way to share data between the workflow and the host. From the host you cannot access directly (eg myWorkflowInstance.MyProperty) Using CallExternalMethod/HandleExternalEvent with an ExternalDataExcahnge service is the way to go. If your data is large than it may be appropriate to save it to a database and then pass a key around via the communication service. If these items that you wish to pass are owned by the workflow then you could save them as UserTrackPoints and then query the TrackingDB for them in your host application. If these do not seem like they apply, please reply with additional details on your scenario and we will take a deeper look. Thanks, Steve Danielson [Microsoft]This posting is provided "AS IS" with no warranties, and confers no rights.
Related Links
WorkFlowRuntime with events (ASp.Net)
Workflow on the fly
Windows Workflow and ASP.Net
State Machine "Undo" to previous state
Bind workflow property to a read-only activity property
Developing an application for a service support provider.
WorkflowRuntime.CreateWorkflow Performance
Access sequential workflow dependency property from custom activity
Access to workflow queues from external processes
Persiste in a custom activity
Workflow re-activates on Final State when using OnWorkflowItemChanged Event
Windows WorkFlow Foundation: Cannot add a new state in between two existing states.
Within VS2008, can't drill down into a workflow without checking it out
SQLTrackingService tracks incorrect WorkflowInstanceEvent order?
XAML Workflows, Property Bindings and Conditions sample
Tracking query timeouts