简体   繁体   中英

Can I cancel a synchronous call to a WebService?

I'm creating a client to a WebService and one of the methods of the WebService is an upload method. The call to this method is done in a BackgroundWorker and a dialog appears with a ProgressBar . But I want the user to be able to abort the upload of the file. I've tried to call the Abort() method. When Abort is called an exception is launched on the BackgroundWorker in the WS upload call, as it was expected. The problem is that most of the times the server completes the call and adds the file to the DB.

Is there another way to abort the WS call?

Once the request is made, you cannot tell the stage at which client would invoke abort - so its quite possible that the request has reached to server and server is already processing it. So even if you could abort from client side, server would already process the request. The same will be applicable in asynchronous web request. A full proof solution is to have compensating transaction ie in this case, abort should invoke call to server to delete the file (server should not raise an error if file does not exists).

VinayC is right, once the service starts, the server process takes over. If you are batching the initiating request or have some loop that you can interrupt, then you can create another service call to cancel the initial request. The cancel request just sets some variable which is checked inside the initiating requests.

But you also want to do the full proof solution mentioned by VinayC because the cancel request may not come in until after the initiating request completes on the server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM