Call webservice when silverlight exit
How i can to call webservice when silverlight exit? I need send update on server w开发者_运维知识库hen 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.
}
}
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);
}
}
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.
Yes, just do the web service call and don't wait for a return value .. because it won't ever arrive
So do this:
private async void Application_Exit(object sender, EventArgs e)
{
// Tell DBSERVER_V14 pipe we have gone away
await connect_disconnect_async(MainPage.username, MainPage.website, false);
}
But don't do this:
private async void Application_Exit(object sender, EventArgs e)
{
// Tell DBSERVER_V14 pipe we have gone away
var status = await SmartNibby_V13.connect_disconnect_async(MainPage.username, MainPage.website, false);
if (status)
{
Console.WriteLine(status);
}
}
because you'll never have a 'status' value with which to test.
精彩评论