The main scenario for this sample is to delay until a specified DateTime using durable timers in a workflow application. This is different from using the built-in Delay activity as this will only allow you to delay for a given TimeSpan (or number of minutes/seconds).
Some real-life scenarios in which you may want to make this distinction include the following:
You want to delay sending a mail for 30 seconds to make sure you didn’t make any errors.
You are working overtime and want to delay all of your mails until normal business hours (such as 8 am).
DurableTimerExtension for implementing Absolute Delay
Setting up persistence using WorkflowApplication for Durable Timers
Use of NativeActivity<TResult> for using Extensibility points
Use of CodeActivity<TResult> in the SendEmail activity
This sample demonstrates how to create a custom activity which takes in a DateTime and uses durable timers to register the delay duration. When using durable timers, you must use a NativeActivity to create a bookmark, as you will need to register this bookmark with the timer extension. In this sample, when the durable timer expires, the
OnTimerExpired method will be called. Make sure that you are adding the timer extension in the CacheMetadata event to ensure you are providing the runtime with this information. The only other implementation detail is that you will need to implement logic to convert from DateTime to TimeSpan, as durable timers only take in a DateTime. Do note that there is a small lapse in accuracy by doing
This sample also demonstrates how to turn on persistence for a WorkflowApplication. For this particular sample, we will be using durable timers in which the workflow data will be unloaded into the persistence database during the idle time while waiting for timer to expire. This implementation can also be used for other persistence actions. This sample shows how to set up the persistence connection string with SQL Server, and how to create the instance store in order to persist the data for workflow instances. Logic is provided on how to resume the workflow once an event is raised which makes the workflow instance runnable.
As you step through this sample, you will see the time in which the built-in delay begins and completes, which in turn will cause an e-mail message to be sent. From there, the AbsoluteDelay activity will halt until a specified DateTime (or 0 seconds if the DateTime has expired) which in turn will send out an email upon expiration. This will show the two different use cases of the built-in Delay functionality versus using an AbsoluteDelay activity.
To set up, build, and run the sample
Ensure you have SQL Server Express (or higher) installed on your machine
Run setup.cmd (from WF/Basic/Services/AbsoluteDelay/CS) in a Visual Studio 2010 command prompt to create the AbsoluteDelaySampleDB database, create the persistence schema and create the persistence logic.
Open the solution in Visual Studio 2010.
Specify the Duration in the Delay activity.
Specify the ExpirationTime in the AbsoluteDelay activity.
Update the SendMailTo, SendMailFrom, SendMailSubject, SendMailBody, and SmtpHost fields in the SendMail activity.
If you do not enter a valid SMTP host, the application will throw a SMTP exception.
Build the solution by selecting Build, Build Solution.
Run the solution by pressing F5.
The samples may already be installed on your machine. Check for the following (default) directory before continuing.
If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.