Windows workflows development
A WorkflowApplication for explicit control over the execution of a single workflow instance. A WorkflowServiceHost for message-based interactions in multi-instance scenarios. Each of these classes wraps the core activity runtime represented as a ActivityInstance responsible for activity execution. There can be several ActivityInstance objects within an application domain running concurrently. Each of the preceding three host interaction objects is created from a tree of activities referred to as a workflow program.
Using these types or a custom host that wraps ActivityInstance , workflows can be executed inside any Windows process including console applications, forms-based applications, Windows Services, ASP. Workflow components in the host process. In the preceding diagram, the Invoke method of the WorkflowInvoker class is used to invoke several workflow instances. WorkflowRuntime defines methods that allow customization of the execution environment. The class also defines events we can listen for during execution.
The CreateWorkflow method returns an object of type WorkflowInstance. The WorkflowInstance class represents an individual workflow. The Start method on the workflow instance object will begin the execution of a workflow. If an exception occurs, the workflow will invoke the Terminate method which leads to the runtime raising a WorkflowTerminated event.
A typical sequence of calls is shown in the screenshot next. The WorkflowRuntime and WorkflowInstance classes are arguably the most important classes needed at run time, but they are not the only classes available.
The WorkflowRuntime class provides only the basic features for executing workflows. Don't worry, these features are available through an extensibility mechanism of WorkflowRuntime — the AddService method.
AddService allows us to make one or more services available to the runtime. These services might be custom services we've written specifically for our domain, like a custom scheduling service, or they might be services already written by Microsoft and included with WF. Let's continue our tour by looking at the services already available.
The DefaultWorkflowSchedulerService creates new threads to execute workflows. Because the threads are separate from the host application, the workflows do not block any application threads and execute asynchronously. The maximum number of simultaneously executing workflows is configurable. Server-side applications typically pull a thread from a pool to service each client request.
It makes sense to loan the thread to the WF runtime, and let the runtime execute the workflow synchronously on the existing request thread instead of using two threads per request, which could reduce scalability. The default transactional service is an instance of the DefaultWorkflowTransactionService class.
Activities inside a running instance of a workflow, and the services operating on the same instance, can all share the same transaction context. WF relies on the implementation of transactions in. NET's System. Transactions namespace. The Transaction class offers a lightweight, auto-enlisting, and promotable transaction. The transaction can start as a local transaction, and later the runtime can promote the transaction to a heavyweight, distributed transaction if needed.
Instead, the runtime can persist the state of the workflow to a durable store and unload the instance from memory. In 30 days or hopefully, less , the runtime can reload the workflow instance and resume processing. The WF runtime will automatically persist a workflow that is idle or suspended when a persistence service is present. Of course, we'll need a database schema that the persistence service understands. A tracking service will tell the runtime the type of information it wants to know about workflows using a tracking profile.
Once the service establishes a profile, the service can open a tracking channel to receive events and data. Chapter 5 includes more details on tracking profiles and channels. The runtime does not start a tracking service by default, but we can programmatically add a tracking service or configure a tracking service with an application configuration file for the runtime to use. Maybe you've had one of those product managers who is always at your desk, asking "are you done, yet?
The sample isn't meant to demonstrate all the capabilities of the platform, but give a general feel for creating and running a workflow with WF. Supported development tools for the. The extensions are not compatible with the Express editions of Visual Studio We'll choose C as our language and select the Sequential Workflow Console Application template see the screenshot on the next page. The template gives us a project with references to all the correct WF assemblies, an empty workflow, and a Program.
Right-click the workflow and select Delete so we can start a workflow from scratch. If we click to expand the node containing Workflow1. As we mentioned earlier, the partial class will combine with the class generated from the XAML to produce a single type. Let's modify the class in Workflow1. If we double-click the. We can drag a While activity from the Toolbox and drop the activity between the start and end point of our workflow. The While Activity executes a child task until some condition is met.
Our next step is to drag a Code activity from the Toolbox into the center of the While activity. At this point, our designer should resemble the following screenshot:.
Notice both activities display a red exclamation point. The activities are failing their validation checks. We can hover the mouse cursor over the exclamation points and open a smart tag to view the validation error. If we tried to compile the program we'd see these same validation errors as compilation errors. We'll fix these errors now. The Code activity requires us to assign an event handler for the ExecuteCode event.
We can set the event by opening the Properties window F4 and clicking the Code activity to set focus. Double-clicking in the empty space beside the ExecuteCode property will send us into the code-beside file and generate a new event handler. We can place the following code into the event handler. This code will ask the user if a bug is fixed, and then read a key press.
The Code activity should now pass validation, so we can turn our attention to the While activity. A While activity requires a valid Condition property. Several activities in the base activity library work with conditions, including the IfElse, ConditionedActivityGroup , and Replicator activities. These choices represent the two techniques available to express a condition, the first being with code a method that returns a Boolean value , the second being with a rule.
Let's select the RuleConditionReference. A rule condition is a named expression that evaluates to true or false, and can live in an external. A plus sign appears beside the Condition property, and we can click the sign to expand the property editor.
When the Condition property expands, the Property window gives us the ability to set a ConditionName and an Expression. The To property will be our Result argument. The Value property can be created with the Expression Editor. The next picture shows the expression. To complete our application, add a reference to the WorkflowLibrary in our web application. We also need the WorkflowInvoker class, so add a reference to System.
Activities to our web application. First we retrieve the entered name from the TextBox. Next we need an instance of our workflow and pass in the username as the In argument. To invoke our Workflow we call the Invoke -method with our workflow instance. It retrieves the Out arguments as an Dictionary , where the key will be a string and the value an object.
As the last thing we just have to get our Result argument out of the Dictionary and assign it to our greeting Label. We have seen how to create a simple workflow with In and Out arguments and how values could be assigned. We have also seen how to invoke a workflow, how to pass in arguments and how to retrieve arguments from the workflow. At the UI side was nothing special and you can work similar in other technologies, such as Silverlight. What is your comments. In my opinion Logic Apps are a good solution in integration scenarios as shown by Scott Hanselmann and Josh Twist in this video.
You might not be able to pause the active downloads or resume downloads that have failed. This training is a complete two day class that takes a real world approach and illustrates how to use Windows Workflow Foundation and. NET 4 Workflow Services to enhance your web applications, providing technical depth to encourage further experimentation and adoption.
Details Note: There are multiple files available for this download. Once you click on the "Download" button, you will be prompted to select the files you need. File Name:. Date Published:. File Size:. System Requirements Supported Operating System.
Install Instructions The course is distributed in two versions: Module by module: see the 11 parts below Grouped by day into two large zip files below Download the desired modules and unzip the files into a directory to view the training.