Jaa


Have you had problems using the FileWatcher sample in a StateMachineWorkflow or WhileActivity?

If you have tried to use the FileWatcherActivity in either a WhileActivity or a StateMachineWorkflow you have probably not had much luck. The changes need to be made to the base activity InputActivity. To get it working in a while activity you need the following change to the ProcessQueueItem method:

From:

WorkflowQueue queue = qService.GetWorkflowQueue(this.QueueName);

if (queue.Count == 0)

{

return false;

}

To:

WorkflowQueue queue = null;

if(qService.Exists(this.QueueName))

{

queue = qService.GetWorkflowQueue(this.QueueName);

}

if (queue == null || queue.Count == 0)

{

return false;

}

Now you you won’t get the following exception after the first file is added to the location the while is subscribed to:

An exception of type 'System.InvalidOperationException' occurred in System.Workflow.Runtime.dll but was not handled in user code

Additional information: Event Queue operation failed with MessageQueueErrorCode QueueNotFound for queue 'Event1'.

With the state machine you get the same error but it is in a different place. This time it is in the Unsubscribe method. To resolve that problem you just need to check to make sure the queue exists before making a call to GetWorkflowQueues like the following:

WorkflowQueuingService qService = context.GetService<WorkflowQueuingService>();

if (!qService.Exists(this.QueueName))

{

return;

}

WorkflowQueue queue = qService.GetWorkflowQueue(this.QueueName);

I have updated the sample with the above changes and made a few other updates to the sample workflow. Now there are two workflows; one sequential and one state machine. The sequential workflow has a invoke workflow activity followed by a while activity with the file watcher. The state machine is the one being invoked and it uses the incoming path parameter to determine where it will be listening. To run the sample create c:\temp1, where the sequential will be listening, and c:\temp2, where the state machine will listen. Now hit F5 and add a new file to each of the directories and look at the output on the console.

If you are using any of the RC builds of WF you need to use the updated sample found here. There have been product changes the will cause build failures for this version of the sample.

This posting is provided "AS IS" with no warranties, and confers no rights.

FileWatcherActivities.exe

Comments

  • Anonymous
    February 22, 2008
    There is a FileWatcher custom WF activity sample that ships with the Windows SDK .&#160;&#160; I initially

  • Anonymous
    July 26, 2011
    Hi Tom, what are the main advantages on using a filesystemwatcher activity than using filesystemwatcher class?