I need to cancel Asynchronous WebService Calls from current page, when user clicks on another item on menu.
Examples on ajax aps.net website use asp button controls to cancel the existing requests. I have html link as menu item, that calls Javascript function which makes 'webservice method calls'. The problem is html controls dose not fire same Sys.WebForms.PageRequestManager
events that asp.net server controls fire.
I can capture invokingrequest event of WebrequestManager that can give me access to executor but it can only abort current request which is not what I want. I want to execute current request but before that cancel existing requests.
How to get access to currently executing requests or requests that are queued by XMLHttp and perform abort or any other operation on them. Is it possible at all.
The only requirement is that instead of using the simplified method for calling web services, you'll have to use a proxy method to do it. Invoking the proxy method returns a WebRequest object which you can save off to a variable. When you make additional
calls, and want to cancel the previous one, you just need to call the WebRequest to get the executor object and call abort() on it.
Here's a simple example. I'm hooking up two buttons to call two different service methods, HelloWorld and GoodByeWorld on WebService1 and WebService2, respectively. Before invoking GoodbyeWorld, we check to see if there is a service currently running using
client script and abort it if it is. Hopefully the sample makes sense to you:
kyus94
Member
13 Points
13 Posts
How to cancel Asynchronous WebService Call
Aug 20, 2008 04:28 PM|LINK
I need to cancel Asynchronous WebService Calls from current page, when user clicks on another item on menu.
Examples on ajax aps.net website use asp button controls to cancel the existing requests. I have html link as menu item, that calls Javascript function which makes 'webservice method calls'. The problem is html controls dose not fire same Sys.WebForms.PageRequestManager events that asp.net server controls fire.
I can capture invokingrequest event of WebrequestManager that can give me access to executor but it can only abort current request which is not what I want. I want to execute current request but before that cancel existing requests.
How to get access to currently executing requests or requests that are queued by XMLHttp and perform abort or any other operation on them. Is it possible at all.
vgiambattista
Star
8370 Points
1283 Posts
Re: How to cancel Asynchronous WebService Call
Aug 21, 2008 06:36 PM|LINK
Kyus,
I hope I'm not misunderstanding your problem. If you need to cancel an webservice call you've made from the client, you can modify the code found here to do it: http://geekswithblogs.net/rashid/archive/2007/07/14/Cancel-a-Web-Service-Call-in-Asp.net-Ajax.aspx
The only requirement is that instead of using the simplified method for calling web services, you'll have to use a proxy method to do it. Invoking the proxy method returns a WebRequest object which you can save off to a variable. When you make additional calls, and want to cancel the previous one, you just need to call the WebRequest to get the executor object and call abort() on it.
Here's a simple example. I'm hooking up two buttons to call two different service methods, HelloWorld and GoodByeWorld on WebService1 and WebService2, respectively. Before invoking GoodbyeWorld, we check to see if there is a service currently running using client script and abort it if it is. Hopefully the sample makes sense to you:
<asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/WebService1.asmx" /> <asp:ServiceReference Path="~/WebService2.asmx" /> </Services> </asp:ScriptManager> <script language="javascript" type="text/javascript"> var lastExecutor;
function helloWorld(){
var service = Sys.Net.WebServiceProxy.invoke('WebService1.asmx' , 'HelloWorld', false, {}, success, fail, '', -1);lastExecutor = service.get_executor();
}
function goodbyeWorld(){
if(lastExecutor != null){
if(lastExecutor.get_started()){
lastExecutor.abort();
}
}
var service2 = WebApplication7.WebService2.GoodbyeWorld(success,fail);}
function success(res){
alert(res);
}
function fail(res){
alert('fail');}
</script> <div> <input id="Button1" type="button" value="button" onclick="helloWorld()" /> <br /> <input id="Button2" type="button" value="button" onclick="goodbyeWorld()" /> </div>James
MVP Client App Dev
kyus94
Member
13 Points
13 Posts
Re: How to cancel Asynchronous WebService Call
Aug 22, 2008 01:04 AM|LINK
Thanks a lot ....that helped