Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Calling web service if browser window is closed RSS

9 replies

Last post Feb 01, 2011 09:16 AM by aliafsal

(0)
  • kobruleht

    kobruleht

    Member

    384 Points

    1083 Posts

    Calling web service if browser window is closed

    Jan 31, 2009 11:39 PM | LINK

    I need to track logged-in users.
    So I need to call web service if user closes browser window by clicking in upper right corner in browser window.

    I tried code below.
    Application_Exit code is executed but service in not called.
    Maybe application is terminated before service call is executed.

    How to call web service if browser window is closed ?

    Andrus.


    public App()
    {
      this.Exit += this.Application_Exit;
    }


    public void Application_Exit(object sender, EventArgs e)
    {
      StockServiceSoapClient service = AWServiceFactory.GetService();
      // this method is not executed in served:
      service.LogoutAsync();
    }

    Microsoft Community Contributor
  • amyo

    amyo

    Star

    8312 Points

    1083 Posts

    Re: Calling web service if browser window is closed

    Feb 01, 2009 07:22 AM | LINK

    check this post: http://silverlight.net/forums/p/20279/130230.aspx

    I also tried like you but its not supported.

    amyo silveright 2

  • kobruleht

    kobruleht

    Member

    384 Points

    1083 Posts

    Re: Calling web service if browser window is closed

    Feb 01, 2009 09:59 AM | LINK

    If it possible to add some sleep call to allow async call to be completed ?

    Or is it possible to send some sync notification like http GET request to server ?

    Microsoft Community Contributor
  • jakkaj

    jakkaj

    Participant

    966 Points

    152 Posts

    Re: Calling web service if browser window is closed

    Feb 01, 2009 10:07 AM | LINK

     Hi,

    This is difficult to acheive and may not even be possible.

    I've had a play around, and unfortunely even with Synchronous web services it just wont work!

    The unload event fires nicely in the browser and is passed to Silverlight (after you subscrive to it using hte HTML DOM Bridge)... This happens on the UI thread. As soon as the method that the JS calls in to reutrns, the browser cuts you off (kills the app). 

    You'd think it would be easy to just make that method never return until the service call has completed, but the problem is that the service call happens on the UI thread also, so if you block the UI thread so it doesn't reutrn form the unload method call too early then the service will never run anyway.

     It's a catch 22 situation :)

    Cheers,

    Jordan.

    http://blog.webjak.net
    http://www.sddn.org.au
    Microsoft MVP - Silverlight
  • kobruleht

    kobruleht

    Member

    384 Points

    1083 Posts

    Re: Calling web service if browser window is closed

    Feb 02, 2009 01:41 PM | LINK

     Have you tried to make sync call in other thread in Exit event ?

    Have you tried to make xmlhttp or sync call from javascript unload event ?

    Andrus.

    Microsoft Community Contributor
  • jakkaj

    jakkaj

    Participant

    966 Points

    152 Posts

    Re: Calling web service if browser window is closed

    Feb 02, 2009 10:44 PM | LINK

     Hi,

    No matter what thread you begin the request on, the network call happens on the UI thread (uses the browsers networking stack and what not, so it makes sense). The wait APM stuff blocks the UI thread. So either you can block UI thread indefinitely or you have to let the exit method exit and that causes the browser to exit anyway.

    Cheers,

     Jordan.

    http://blog.webjak.net
    http://www.sddn.org.au
    Microsoft MVP - Silverlight
  • hariram.p@live.in

    hariram.p@li...

    Member

    181 Points

    50 Posts

    Re: Calling web service if browser window is closed

    Mar 15, 2009 07:49 AM | LINK

    You call the Web Service Method from javscript using XMLHTTP Request.

    This method can be called in body unload event.

    Thanks
    Hari
    ---------------------------------------------
    Dont forget to click "Mark as Answer" on the post that helped you.

    My Site
  • aliafsal

    aliafsal

    Member

    6 Points

    5 Posts

    Re: Calling web service if browser window is closed

    Jan 31, 2011 07:12 AM | LINK

    Any updates to this issue..? :(

  • syed amjad

    syed amjad

    Star

    8553 Points

    1615 Posts

    Re: Calling web service if browser window is closed

    Jan 31, 2011 01:45 PM | LINK

    I have small solution for you which i implemented in my project, which my might not be the optimize solution but something is better than nothing.

    if user closes browser window by clicking in upper right corner in browser window, show a message box using Javascript for taking confirmation with ok button, while user clicks the ok button, we will get the enough time to call the webservice.

    You can implement this or similar one

    Thanks & Regards
    Syed Amjad
    Silverlight Developer
    follow me on skype : syed.amjad0786
    Mark as Answer", if it helped you or it might help others too.
  • aliafsal

    aliafsal

    Member

    6 Points

    5 Posts

    Re: Calling web service if browser window is closed

    Feb 01, 2011 09:16 AM | LINK

    Hi Syed,

    In my case when user clicks close button a confirmation box will appear and if user clicks OK , need to save the unsaved data and close the application. But my application closes without waiting for the result of user click(obviously it is an async call).

    Any help will be greatly appreciated...

    --thanks,

    Afsal