Click here to Skip to main content
15,884,975 members
Articles / Web Development / ASP.NET

Scheduling Tasks in ASP.NET using Workflows

Rate me:
Please Sign up or sign in to vote.
4.50/5 (5 votes)
23 Jun 2011CPOL4 min read 53.4K   1.6K   37   6
One of the many ways of scheduling tasks in ASP.NET

Introduction

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:

workflow.JPG

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.

XML
 <Activity mc:Ignorable="sap" 
	x:Class="WorkflowConsoleApplication.WorkflowWithScheduler" 
          sap:VirtualizedContainerService.HintSize="526,702" 
          mva:VisualBasic.Settings="Assembly references and imported namespaces 
		for internal implementation" 
          xmlns="<a href="http://schemas.microsoft.com/netfx/2009/xaml/activities">
		http://schemas.microsoft.com/netfx/2009/xaml/activities</a>" 
          xmlns:local="clr-namespace:WorkflowConsoleApplication" 
          xmlns:mc="<a href="http://schemas.openxmlformats.org/markup-compatibility/2006">
		http://schemas.openxmlformats.org/markup-compatibility/2006</a>" 
          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" 
		xmlns:s1="clr-namespace:System;assembly=System" 
		xmlns:s2="clr-namespace:System;assembly=System.Xml" 
		xmlns:s3="clr-namespace:System;assembly=System.Core" 
		xmlns:sad="clr-namespace:System.Activities.Debugger;
		assembly=System.Activities" 
		xmlns:sap="<a href="http://schemas.microsoft.com/netfx/2009/
		xaml/activities/presentation">http://schemas.microsoft.com/
		netfx/2009/xaml/activities/presentation</a>" 
		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:sl="clr-namespace:System.Linq;assembly=System.Core" 
		xmlns:st="clr-namespace:System.Text;assembly=mscorlib" 
		xmlns:x="<a href="http://schemas.microsoft.com/winfx/2006/xaml">
		http://schemas.microsoft.com/winfx/2006/xaml</a>">
  <Sequence sad:XamlDebuggerXmlReader.FileName="C:\Users\Chinna\documents\
	visual studio 2010\Projects\SchedulerSample\WorkflowConsoleApplication1\
	WorkflowWithScheduler.xaml" sap:VirtualizedContainerService.HintSize="486,657">
    <sap:WorkflowViewStateService.ViewState>
      <scg3:Dictionary x:TypeArguments="x:String, x:Object">
        <x:Boolean x:Key="IsExpanded">True</x:Boolean>
      </scg3:Dictionary>
    </sap:WorkflowViewStateService.ViewState>
    <While sap:VirtualizedContainerService.HintSize="464,533" Condition="True">
      <Sequence sap:VirtualizedContainerService.HintSize="438,417">
        <sap:WorkflowViewStateService.ViewState>
          <scg3:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg3:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
        <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" />
      </Sequence>
    </While>
  </Sequence>
</Activity></span>

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.
C#
System.Workflow.Runtime.WorkflowRuntime
workflowRuntime = new System.Workflow.Runtime.WorkflowRuntime();
System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService
manualService = new
System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService();
workflowRuntime.AddService(manualService);
workflowRuntime.StartRuntime();
Application["WorkflowRuntime"] =
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.

XML
System.Workflow.Runtime.WorkflowRuntime workflowRuntime = 
	Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
workflowRuntime.StopRuntime();</span>

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.

Advantages

  • 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

Disadvantages

  • 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.

History

  • 23rd June, 2011: Initial post

License

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


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionexception when running Pin
jc.net27-Jun-11 0:54
jc.net27-Jun-11 0:54 
AnswerRe: exception when running Pin
Vivek Krishnamurthy27-Jun-11 21:21
Vivek Krishnamurthy27-Jun-11 21:21 
GeneralMy vote of 5 Pin
adatapost24-Jun-11 2:34
adatapost24-Jun-11 2:34 
QuestionI can't download! Pin
Chit Hein23-Jun-11 16:55
Chit Hein23-Jun-11 16:55 
AnswerRe: I can't download! Pin
Vivek Krishnamurthy23-Jun-11 20:18
Vivek Krishnamurthy23-Jun-11 20:18 
GeneralRe: I can't download! Pin
Chit Hein23-Jun-11 20:38
Chit 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.