Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

microsoft press windows workflow foundation step by step phần 5 pps
Nội dung xem thử
Mô tả chi tiết
142 Part II Working with Activities
2. Click the View Fault Handlers button to activate the fault handlers visual designer. The
View Fault Handlers button is the right button of the three-button group at the lower-left
corner of the designer window. The designer should appear as you see here:
3. Select the FaultHandler activity from the Toolbox, drag it over the workflow designer’s
fault handler surface, and drop it between the blue arrows.
Your designer surface should now look like this:
4. As with other activities, we have some properties to set to make this fault handler fully
operational. The first property we’ll set is the FaultType property. Select the FaultType
Chapter 7 Basic Activity Operations 143
property in Visual Studio’s Properties pane, and click the browse button (the button
with the three dots) to activate the Browse And Select A .NET Type dialog box.
5. With the Browse And Select A .NET Type dialog box active, select the Type tab if it’s not
already selected and then expand the mscorlib tree node. From there, select System (for
the System assembly) and scroll down the resulting list in the right pane until you find
the list item Exception in the Type Name column. Select this by clicking the Exception
line in the right pane’s ListView control. This places the text “System.Exception” in the
Type Name edit control. Click OK to accept System.Exception as the exception type and
dismiss the dialog box. You should find the value System.Exception has been assigned to
the FaultType property.
Note It’s no coincidence that the exception we’re asking this FaultHandler activity
to use is the same as the exception type thrown by the Throw activity we used earlier
in the chapter. They’re a matched set. If you don’t have a corresponding fault handler
for a Throw activity in your workflow, keep in mind that if the exception is thrown at
runtime, the WorkflowTerminated event is where you’ll soon find your workflow
executing. If this isn’t what you want, add the appropriate FaultHandler activity.
144 Part II Working with Activities
Note Although you see the Fault property in the preceding graphic, it’s actually
disabled and therefore cannot be set. Ignore it.
6. So far, we’ve added a FaultHandler activity and we’ve told it what type of exception it will
be handling, but we’ve not actually provided any code to deal with the exception if it’s
thrown. To do that, drag a Code activity from the Toolbox and drop it into the area below
where we dropped the FaultHandler activity itself. This area, identified by the name
faultHandlerActivity1, is like a miniature workflow visual designer. So it readily accepts
the Code activity, and as we’ve done with other instances of the Code activity, assign a
value to its ExecuteCode property. In this case, type in OnException and press Enter.
7. Visual Studio then adds the OnException event handler to Workflow1 and opens the code
editor for editing. To the OnException event handler, add this code:
Console.WriteLine(
"Exception handled within the workflow! The exception was: '{0}'",
WorkflowException != null ? WorkflowException.Message :
"Exception property not set, generic exception thrown");