views:

61

answers:

4

How i can to call webservice when silverlight exit? I need send update on server when silverlight exit.

+3  A: 

Add an event handler for the Application.Exit event. Call the WebService in that handler. The XAML/Code looks something like:

<Application 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="SilverlightApplication.App"
    Exit="App_Exit">

</Application>

And

public partial class App : Application
{
    private void App_Exit(object sender, EventArgs e)
    {
        // Code to call WebService goes here.
    }
}
Justin Niessner
in webservice async methods. For example: method save(). In services saveAsync(); and saveCompleted(). saveAsync is execute, but saveCompleted not execute if using your answer.
Mikhail
@Mikhail do you mean that you aren't getting the callback event? Since it is async this is probably because your app has already closed before the return from the service. Do you need a return value?
Andy May
Yes exactly....
Mikhail
It is not possible to get a return value. App_Exit will be called just before close, but the application shuts down as soon as you leave the `App_Exit` method, so it will be closed before the service call returns.
Stephan
A: 

See the comments on Justin Niessner's answer: you can't get back the return value. That may be OK for you if the service that you're calling is not critical (because, let's say, it just captures some usage statistics). If you need to have the return value in any case, and you expect the SL application to be used multiple times, you could write a memento to the IsolatedStorage (that is a synchronous operation) and post it to the server when the application starts the next time.

GreenIcicle
+1  A: 

You can't make a web request on application shutdown in Silverlight.

Michael S. Scherotter
A: 

I had an application that needed to save information before closing. I used javascript in the page hosting the silverlight control.

Javascript and usage

<script type="text/javascript">
     var blocking = true;

     function pageUnloading() {
         var control = document.getElementById("Xaml1");
         control.content.Page.FinalSave();
         while (blocking)
             alert('Saving User Information');
     }

     function allowClose() {
         blocking = false;
     }
</script>


<body onbeforeunload="pageUnloading();">

</body>

and app.xaml.cs

public partial class App : Application
{

     [ScriptableMember()]
     public void FinalSave()
     {

        srTL.TrueLinkClient proxy = new CSRM3.srTL.TrueLinkClient();
        proxy.DeleteAllUserActionsCompleted += (sender, e) =>
            {
                HtmlPage.Window.CreateInstance("allowClose");


            };
        proxy.DeleteAllUserActionsAsync(ApplicationUser.UserName);

     }

}
bigred242