Hi Rajeev,
There are a couple of ways to go about this. I've *THROWN* together a
simple example to give you some idea how you might go about this. This
certainly isn't exactly the approach I'd take for these reasons...
1. The Application dictionary is used to manage the cancellation. This
is application wide and means that this web method can only be called
by any one client at a time.
2. It wouldn't work on a web farm because the Application dictionary is
local to the machine.
3. The client of this service would need to call the
DoLongRunningActivity in an asynchronous manner such that it could
still call the CancelActivity method.
However, the core concepts are there so I hope you find this of some
use.
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public int DoLongRunningActivity()
{
if (CurrentState == State.Running)
{
throw new InvalidOperationException(
"Method cannot be called concurrently from the same
session");
}
CurrentState = State.Running;
int i = 0;
for (i = 0; i < 30; i++)
{
if (CurrentState == State.Abort)
{
// Jump out of the loop
break;
}
// pretend to do some work....
System.Threading.Thread.Sleep(1000);
}
CurrentState = State.Idle;
return i;
}
[WebMethod(EnableSession=true)]
public void CancelActivity()
{
CurrentState = State.Abort;
}
private State CurrentState
{
set
{
Application["state"] = value;
}
get
{
if (Application["state"] == null)
{
Application["state"] = State.Idle;
}
return (State)Application["state"];
}
}
private enum State
{
Idle,
Running,
Abort
}
}
_______________
If I was to write such a system myself I'd probably have a method
called StartLongRunningProcess() that returned a unique activityId
instantly. It would start the actual processing work on a new thread. A
database table would be used to manage the state of activities and a
client could cancel an activity by calling a CancelActivity(int
activityId) method.
So that the client could be kept abreast of the status of the activity
I'd have a third method that 'polled' the service for an update, maybe
GetActivityProgress(int activityId) which returned a percentage
complete if that was possible.
If I was using WCF (indigo) as the framework for services, I'd look to
see if there is anyway I could make my client listen to events inside
the web service.
HTH
Josh
http://www.thejoyofcode.com/