Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Stop running web method

1 view
Skip to first unread message

Rajeev

unread,
Mar 20, 2006, 7:08:17 AM3/20/06
to
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

Josh Twist

unread,
Mar 20, 2006, 10:14:30 AM3/20/06
to
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/

Rajeev

unread,
Mar 21, 2006, 6:14:07 AM3/21/06
to
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

0 new messages