How i can to call webservice when silverlight exit? I need send update on server when silverlight exit.
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.
}
}
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.
You can't make a web request on application shutdown in Silverlight.
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);
}
}