Workflow based web based windows service
One of the most common questions in web development forums is - "how can I open a page/call a web service/ run a sql query / write to file / send email every X time from my web application".
The most common answers are:
1. Use a scheduler on the server to run a windows application or open a web page which will run the job.
2. Write a windows service which will run the job
Both answers require access to the server, usually beyond the access level hosting companies allow.
After reading another question of that type at tapuz .net forum I tried to create a small workflow service which will run inside the iis process.
The workflow looks like this:
A very basic workflow which has an endless while loop with a delay activity and a code activity which "does the job".
The code in the web application is:
System.Workflow.Runtime.WorkflowRuntime wfruntime;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
wfruntime=new WorkflowRuntime();
wfruntime.CreateWorkflow(typeof(webservice.Workflow1)).Start();
}
Which simply creates an instance of a workflowruntime in the application level and stats a single instance of the above workflow.
The current job simply writes an entry to the event log which looks like this after 5 minutes: (the delay is set to 1 minute)
Update: (14/8/2007)
In case your website’s traffic is very slow you might need a different solution which will make sure the server is constantly receiving requests in order to keep the application process alive:
Here there are 2 loops, one main endless loop which makes sure the workflow will never end.
In this loop there is a parallel activity with two branches – one with the second loop which is doing the required job and the second with a delay and code activity which sends a request to the server using the webclient object.
There is a bool dependency property called ExitLoop in the background which is set to false at every round of the main loop and then to true every time the server is being pinged by the webclient object. The second loop is running as long as ExitLoop is set to false.
This way the process is kept alive, and by playing with the delay values you can easily schedule tasks on your web application.