In this article, I would like to demonstrate one of the many approaches of scheduling tasking. In this case, I am taking an ASP.NET application using workflow foundation. However, the preferred way to implement is to use Task scheduler or Windows service as your primary option.
I came across these articles that explain different approaches for implementing scheduler in ASP.NET:
Consideration for Scheduling Job
When we think about a solution for scheduled jobs, Windows services and Windows Task scheduler comes to our mind. If are interested in evaluating which one is better, take a look at the following blog:
Deployment – In case of both task scheduler and windows scheduler, we will need to have deployment package / scripts written. In case of any changes to the tasks, we will have to update all the servers running the service or tasks.
Maintenance – Every scheduled job usually turns out to be a separate application (mostly console). Maintaining these applications becomes tedious as the number of such jobs increase. However, we always have exceptions. Also, handling changes to password is a bit of a problem with Task scheduler. However, you might be able to write some scripts if you would like to automate this.
Control - Scheduling tasks to run within seconds could be a challenge in Task Scheduler.
Security - Note from MSDN: A user for whom you assign permissions to the Tasks folder using cacls will be able to access scheduled tasks for all users. Choose which users to give access to the Tasks folder judiciously.
Error Handling and Notification – If the tasks fail to launch and ends up in error, we will need to implement the Error reporting in every job.
Using the Code
We directly get into some code.
Step 1: Create your ASP.NET application
I would not get into details of the first step. If you are new to ASP.NETnet you can refer to ASP.NET.
Open Visual studio 2010, select New Project, select ASP.NET Web Application, enter the name for the project and Click Ok.
Step 2: Create your choice of Workflow Application
- Right click on the solution, Select Add -> New Project…
- Workflow -> Workflow Console Application
Step 3: Create your Workflow, it should have a job to do and a timer to pause / sleep, inside a continuous
- You can use the default workflow that is created with your application or create a new one.
- Add a
While activity with condition set to
- Add a sequential activity inside
- Add an
Invoke method activity into the Sequential activity
- Configure the class and method parameters, method should have
Public Static access modifier.
- Add a
Invoke method activity into the Sequential activity.
- Set the timer to appropriate interval.
This is how the activity should now look:
XAML code for the above workflow would be as follows.
For the purpose of example, I am writing to debug in the interval of 10 seconds. You can change the
InvokeMethod activity to invoke an appropriate method on your respective business or service component.
mva:VisualBasic.Settings="Assembly references and imported namespaces
for internal implementation"
Step 4: Setting up and starting workflow runtime
Loading and starting the Workflow Runtime is done inside the
- In the
Application_Start event, add the following code. We add a reference to the
WorkflowRuntime object we created to
Application objects collection.
workflowRuntime = new System.Workflow.Runtime.WorkflowRuntime();
manualService = new
Step 5: Start workflow when ASP.NET application starts.
Following the above code in
Application_Start event, you can start the workflow service, if this is expected to start when the application starts. In this code, we are creating an instance of the workflow of type
WorkflowConsoleApplication1.Workflow1 and starting the same.
Step 6: Stop workflow service when the ASP.NET application stops.
Add the following code in
Application_End event to stop the workflow runtime.
System.Workflow.Runtime.WorkflowRuntime workflowRuntime =
Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
Step 7: Testing the scheduler using ASP.NET.
Verify the workflow has done the task you expected it to do.
Step 8: Start / Stop workflow conveniently
If you do not wish to start the workflow in the
Application_Start event, the same can be performed on click of a button in one of your web pages. Access to this can be provided by the application’s authentication and authorization.
- No need for separate deployment pack for the scheduler
- WF Tracking Services
- No need for additional configuration or maintenance of the scheduler
- Runs in the security context of the worker process
- Role based access can be implemented to allow control over workflow
- Server Administrator’s intervention won’t necessary
- Not a conventional way of handling Scheduled Tasks
- Hosting Infrastructure should be taken into account while designing Workflow enabled ASP.NET application due to the key architectural difference.
- Design of the application should be done keeping maintainability of the application in mind.
- 23rd June, 2011: Initial post