OnRequestCompleted回调中异常的全局处理

时间:2016-06-23 16:39:04

标签: c# wpf exception-handling nest unhandled

如果对Elasticsearch的任何请求失败,我想展示一个道歉的消息并关闭我的WPF应用程序。在应用程序中有很多请求,所以我认为最方便的方法是在出现问题时在OnRequestCompleted回调中抛出异常,然后在全局处理它。但是处理未处理的异常的4种方法中的每一种都列出here什么都不做,应用程序就崩溃了。这是最简单的例子。那里有什么不对?

的App.xaml

<Application x:Class="WPF_Elasticsearch_2._3.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             DispatcherUnhandledException="App_OnDispatcherUnhandledException"
             StartupUri="MainWindow.xaml">
</Application>

App.xaml.cs

using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Nest;

namespace WPF_Elasticsearch_2._3
{
    public partial class App : Application
    {
        public static IElasticClient ElasticClient { get; private set; }

        public App()
        {
            AppDomain.CurrentDomain.UnhandledException += App_OnUnhandledException;
            TaskScheduler.UnobservedTaskException += App_OnUnobservedTaskException;
            Dispatcher.UnhandledException += App_OnDispatcherUnhandledException;

            var connectionSettings = new ConnectionSettings(new Uri("http://localhost:9200"))
                .OnRequestCompleted(callDetails =>
                {
                    if (!callDetails.Success)
                        throw new Exception("Unsuccessful request!");
                });

            ElasticClient = new ElasticClient(connectionSettings);
        }

        private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
            e.Handled = true;
            Shutdown(-1);
        }

        private void App_OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show(((Exception)e.ExceptionObject).Message);
            Shutdown(-1);
        }

        private void App_OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
            e.SetObserved();
            Shutdown(-1);
        }
    }
}

MainWindow.xaml

<Window x:Class="WPF_Elasticsearch_2._3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="240" Width="320">

    <Button Click="ButtonBase_OnClick">Make request</Button>
</Window>

MainWindow.xaml.cs

using System.Windows;

namespace WPF_Elasticsearch_2._3
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            App.ElasticClient.Search<object>(descriptor => descriptor
                .Index("test-index")
                .Type("test-type"));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以configure the client to throw on exceptions使用

Cursor cursor = db.rawQuery("SELECT _id, value FROM chosen ORDER BY _id", null);

通过这种方式,您无需在var connectionSettings = new ConnectionSettings(new Uri("http://localhost:9200")) .ThrowExceptions(); var client = new ElasticClient(connectionSettings); 中抛出异常。

正如其他人在评论中所说,你仍然需要以某种方式处理这些例外;如果使用.OnRequestCompleted(),您可以将您的调用包装在.ThrowExceptions()块中,在try/catch中捕获Elasticsearch异常时向UI发送消息。