Click here to Skip to main content
13,190,405 members (53,105 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 23 Jun 2011

Scheduling Tasks in ASP.NET using Workflows

, 23 Jun 2011
Rate this:
Please Sign up or sign in to vote.
One of the many ways of scheduling tasks in ASP.NET


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

  1. Right click on the solution, Select Add -> New Project…
  2. 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 while loop.

  1. You can use the default workflow that is created with your application or create a new one.
  2. Add a While activity with condition set to True.
  3. Add a sequential activity inside While activity.
  4. Add an Invoke method activity into the Sequential activity
  5. Configure the class and method parameters, method should have Public Static access modifier.
  6. Add a Timer after Invoke method activity into the Sequential activity.
  7. 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.

 <Activity mc:Ignorable="sap" 



          mva:VisualBasic.Settings="Assembly references and imported namespaces 
		for internal implementation" 

          xmlns="<a href=""></a>" 
          xmlns:mc="<a href=""></a>" 
		xmlns:sap="<a href="
		assembly=System" xmlns:scg1="clr-namespace:System.Collections.
		assembly=System.Core" xmlns:scg3="
		xmlns:x="<a href=""></a>">
  <Sequence sad:XamlDebuggerXmlReader.FileName="C:\Users\Chinna\documents\
	visual studio 2010\Projects\SchedulerSample\WorkflowConsoleApplication1\
	WorkflowWithScheduler.xaml" sap:VirtualizedContainerService.HintSize="486,657">
      <scg3:Dictionary x:TypeArguments="x:String, x:Object">
        <x:Boolean x:Key="IsExpanded">True</x:Boolean>
    <While sap:VirtualizedContainerService.HintSize="464,533" Condition="True">
      <Sequence sap:VirtualizedContainerService.HintSize="438,417">
          <scg3:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
        <InvokeMethod sap:VirtualizedContainerService.HintSize="218,130" 

		MethodName="WriteDebug" TargetType="local:MyClass" />
        <WriteLine sap:VirtualizedContainerService.HintSize="218,61" Text="testing it..." />
        <Delay Duration="00:00:10" sap:VirtualizedContainerService.HintSize="218,22" />

Step 4: Setting up and starting workflow runtime

Loading and starting the Workflow Runtime is done inside the Application_Start event.

  1. Open Global.asax.cs file
  2. 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
Application["WorkflowRuntime"] =

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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vivek Krishnamurthy
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Questionexception when running Pin
jc.net27-Jun-11 0:54
memberjc.net27-Jun-11 0:54 
AnswerRe: exception when running Pin
Vivek Krishnamurthy27-Jun-11 21:21
memberVivek Krishnamurthy27-Jun-11 21:21 
GeneralMy vote of 5 Pin
kanu@adatapost24-Jun-11 2:34
memberkanu@adatapost24-Jun-11 2:34 
QuestionI can't download! Pin
Chit Hein23-Jun-11 16:55
memberChit Hein23-Jun-11 16:55 
AnswerRe: I can't download! Pin
Vivek Krishnamurthy23-Jun-11 20:18
memberVivek Krishnamurthy23-Jun-11 20:18 
GeneralRe: I can't download! Pin
Chit Hein23-Jun-11 20:38
memberChit Hein23-Jun-11 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171016.2 | Last Updated 23 Jun 2011
Article Copyright 2011 by Vivek Krishnamurthy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid