【问题标题】:How to stop the running wcf services in silverlight when exception happens发生异常时如何在silverlight中停止正在运行的wcf服务
【发布时间】:2010-06-18 21:38:34
【问题描述】:

在深入研究了 silverlight 中的异常处理并阅读了一些类似这样的有用博客之后 Silverlight exception handling using WCF RIA Services and WCF Services 我最终在 App.xaml.cs 中实现了类似的想法,以显示错误页面并调用另一个 wcf 服务方法将错误记录到事件查看器:

 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        if (!System.Diagnostics.Debugger.IsAttached)
        {
            var errorPage = new Error();
            errorPage.Show();

            string errorMsg = string.Format("{0} {1}", e.ExceptionObject.Message, e.ExceptionObject.StackTrace);
            EventHandler<WriteIntoEventLogCompletedEventArgs> callback = (s, ev) =>
            {
                bool result = ev.Result;
            };
            (new ServiceProxy<ApplicationServiceClient>()).CallService<WriteIntoEventLogCompletedEventArgs>(callback, errorMsg);

            e.Handled = true;
        }
    }

这就是我在 Error.xaml.cs 中的内容:

 private void OKButton_Click(object sender, RoutedEventArgs e)
 {
        this.DialogResult = true;
 }

当用户点击确定时,基本上会关闭错误页面。

在大多数情况下一切正常。当对 wcf 服务的回调之一导致异常时,就会出现问题。错误页面将很好地显示,当用户单击确定时,错误页面将关闭。但是后台仍然显示忙碌指示,原始服务回调仍在等待响应。我需要以某种方式终止它。

如果有人可以提供帮助,我将不胜感激。

谢谢, 西尔

--

非常感谢您的帮助回复。我使用了相同的想法,并在原始服务回调方法中添加了一个代码来检查 e.Error,如果它不为空,请关闭窗口(它是一个子窗口) busyindicator,现在一切正常。再次感谢。西尔

【问题讨论】:

    标签: silverlight exception-handling


    【解决方案1】:

    我的猜测是原始服务回调可能正在完成但处于错误状态。您可能需要检测错误情况并将 busyindicator 的 IsBusy 属性设置回 False。

    需要检查的几件事

    • 至少原始服务回调是否成功返回?您可以通过在原始服务回调方法中放置断点来检查这一点。

    • 您是否正确处理了回调方法中的错误情况。例如 -

    
    void proxy_GetUserCompleted(object sender, GetUserCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                getUserResult.Text = "Error getting the user.";
            }
            else
            {
                getUserResult.Text = "User name: " + e.Result.Name + ", age: " + e.Result.Age + ", is member: " + e.Result.IsMember;
            }
        }
    

    参考 - http://msdn.microsoft.com/en-us/library/cc197937(v=VS.95).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 2011-03-20
      相关资源
      最近更新 更多