Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / March 2006

Tip: Looking for answers? Try searching our database.

Stop running web method

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rajeev - 20 Mar 2006 13:08 GMT
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 - 20 Mar 2006 16:14 GMT
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 - 21 Mar 2006 12:14 GMT
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

> Hi Rajeev,
>
[quoted text clipped - 102 lines]
> Josh
> http://www.thejoyofcode.com/

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.