C# silverlight退出时调用webservice

C# silverlight退出时调用webservice,c#,silverlight,web-services,C#,Silverlight,Web Services,silverlight退出时如何调用webservice?silverlight退出时,我需要在服务器上发送更新。为事件添加事件处理程序。在该处理程序中调用WebService。XAML/代码如下所示: <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

silverlight退出时如何调用webservice?silverlight退出时,我需要在服务器上发送更新。

为事件添加事件处理程序。在该处理程序中调用WebService。XAML/代码如下所示:

<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>

请参阅Justin Niessner回答的评论:您无法获取返回值。如果您正在调用的服务不重要(因为,比方说,它只捕获了一些使用统计数据),那么这对您来说可能没问题。如果您在任何情况下都需要返回值,并且希望SL应用程序被多次使用,那么您可以向IsolatedStorage写入memento(这是一个同步操作)下次应用程序启动时将其发布到服务器。

在Silverlight中,应用程序关闭时不能发出web请求。

我有一个应用程序需要在关闭前保存信息。我在承载silverlight控件的页面中使用了javascript

Javascript及其使用

<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>

是的,只需执行web服务调用,不要等待返回值。。因为它永远不会到来

这样做:

    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);
    }
但不要这样做:

    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);
        }
    }

因为在webservice异步方法中,您永远不会有一个“status”值来进行测试。

。例如:方法save()。在服务中saveAsync();和saveCompleted()。saveAsync是execute,但是如果使用您的答案,saveCompleted不是execute。@Mikhail您的意思是您没有收到回调事件吗?由于它是异步的,这可能是因为您的应用程序在从服务返回之前已经关闭。是否需要返回值?无法获取返回值。App_Exit将在关闭前被调用,但一旦您离开
App_Exit
方法,应用程序就会关闭,因此它将在服务调用返回之前关闭。不同浏览器上出现的javascript while块会发出警报,表示正在运行的脚本已失控。。。除此之外,这似乎有效
    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);
    }
    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);
        }
    }