Stop running web method

R

Rajeev

Hi,

I written a Web Service using C#. My one web method takes longer time
(10-15 mins) to execute. I want to give stop process facility from
client application. I am inserting 400-500k rows in database inside
that method. I want to stop that insertion process, so I need to pass a
flag from client to that method.

Anybody can suggest how to set any flag to my web service or if any
other option is available to stop that process.

Even I close my client application then also that web method keeps
running.

Thanks in advance.

Regards
Rajeev
 
J

Josh Twist

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/
 
R

Rajeev

Thanks Josh,

My web service used by many clients at a time, so I can't use
Application object to maintain state. I try your another approach.

Thanks & Regards
Rajeev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top