Microsoft supplies many activities out of the box, but you will want to and should create your own activities. Activities can be created in three main ways:
As a composition of other existing activities
In code
In pure XAML
Let's create a simple activity to simulate writing a customer's ticket booking to a database:
Right-click the project and add a new activity (from the Workflow section) called SaveBooking.xaml.
Open SaveBooking.xaml in design view.
We are not going to go to the bother of actually writing data access code, so change the Value property of the WriteLine activity to "Save booking" + BookingReference + "in database". Your workflow should look like Figure 6-12 (note the quotes).
Before this activity can be used, the project needs to be compiled. Compile the project.
On the properties of the SaveBooking activity, set the BookingReference argument to use the BookingReference variable defined in the main workflow.
Run your workflow. You should now see the output "Save booking" and whatever you have set the booking reference variable to.
Anything that can be done in the WF designer can also be done programmatically. Let's say our client is running a new promotion that every theater booking has a 1 in 100 chance of being free. We will create a new code activity to simulate this.
Amend the code in FreeBookingPromotion.cs to the following:
public class FreeBookingPromotion : CodeActivity { public InArgument<string> BookingReference { get; set; } public OutArgument<bool> FreeBooking { get; set; } protected override void Execute(CodeActivityContext context) { System.Random Random = new Random(); if (Random.Next(1, 100) == 100) { //Customer has won free booking FreeBooking.Set(context, true); } else { FreeBooking.Set(context, false); } } }
Create a new variable at Workflow1.xaml level called FreeBooking, of type Boolean.
Modify the activity's FreeBooking argument to use the main workflow's FreeBooking variable.
Double-click the If activity.
In the condition, enter freebooking=true.
In the Else block, drag a WriteLine activity and change the text to "Sorry try again next time."
Run your workflow.
Activities can also be created purely in XAML, which is great news for third-party vendors and code generation. You could even hold your activities' XAML definitions in a database and load them at runtime. Let's take a look at the XAML of our SaveBooking activity.
Select XML Editor, and you'll be able see the XAML that makes up this activity:
<Activity mc:Ignorable="sap" x:Class="Chapter6.HelloWF.SaveBooking" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr- namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr- namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr- namespace:System;assembly=mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" xmlns:s1="clr-namespace:System;assembly=mscorlib" xmlns:s2="clr-namespace:System;assembly=System" xmlns:s3="clr- namespace:System;assembly=System.Xml" xmlns:s4="clr-namespace:System;assembly=System.Core" xmlns:sa="clr-namespace:System.Activities;assembly=System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" xmlns:sad="clr- namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr- namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr- namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr- namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr- namespace:System.Data;assembly=System.Data" xmlns:sd1="clr- namespace:System.Data;assembly=System.Data.DataSetExtensions" xmlns:sl="clr- namespace:System.Linq;assembly=System.Core" xmlns:st="clr- namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <x:Members> <x:Property Name="BookingReference" Type="InArgument(x:String)" /> </x:Members> <mva:VisualBasic.Settings>Assembly references and imported namespaces serialized as XML namespaces</mva:VisualBasic.Settings> <Sequence sad:XamlDebuggerXmlReader. FileName="D:wwwrootookChapter6_WFChapter6.HelloWFChapter6. HelloWFSaveBooking.xaml"
sap:VirtualizedContainerService.HintSize="233,200"> <sap:WorkflowViewStateService.ViewState> <scg3:Dictionary x:TypeArguments="x:String, x:Object"> <x:Boolean x:Key="IsExpanded">True</x:Boolean> </scg3:Dictionary> </sap:WorkflowViewStateService.ViewState> <WriteLine DisplayName="Simulate booking storage" sap:VirtualizedContainerService.HintSize="211,62" Text="["Store booking " + BookingReference + "in database"]" /> </Sequence> </Activity>
Yikes, probably not the sort of thing you want to be typing yourself.
18.190.253.43