简体   繁体   English

c#wpf如何捕获FirestoreChangeListener异常

[英]c# wpf How to catch FirestoreChangeListener exceptions

I am creating several FirestoreChangeListeners (following the User Guide at https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Firestore/userguide.html ) and everything is working fine, but when there is a network problem, a System.AggregateException is being thrown with the inner exception being a Grpc.Core.RpcException with Status of "Transport Closed" or "Failed to connect" (This makes sense).我正在创建几个FirestoreChangeListeners (遵循https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Firestore/userguide.html 上的用户指南)并且一切正常,但是当有网络问题,一个 System.AggregateException 被抛出,内部异常是一个 Grpc.Core.RpcException,状态为“传输关闭”或“连接失败”(这是有道理的)。

The problem is I can't figure out how to catch or handle these inner exceptions, which are thrown by the FirestoreChangeListeners, or how I should structure the method to do be able to do this.问题是我不知道如何捕获或处理这些由 FirestoreChangeListeners 抛出的内部异常,或者我应该如何构造方法才能做到这一点。

I've tried adding try/catch in multiple places, tried adding ContinueWith to the listener, but regardless I still get unhandled System.AggregateException.我尝试在多个地方添加 try/catch,尝试将 ContinueWith 添加到侦听器,但无论如何我仍然得到未处理的 System.AggregateException。

在此处输入图片说明

The documentation says the Listen method is a "convenience" method that supposedly helps in monitoring the Listener... and it does include a ListenerTask method that returns a Task.文档说,Listen 方法是一种“方便”的方法,据说有助于监视监听器......它确实包含一个返回任务的 ListenerTask 方法。 Perhaps I need to do something with these tasks... Everything I have read suggestions I should await this, but since it's an async listener that runs in the background for the whole time the program is running, I'm not sure what method should await... wouldn't await block the whole method?也许我需要对这些任务做些什么......我读过的所有建议我都应该等待,但是由于它是一个异步侦听器,在程序运行的整个时间都在后台运行,我不确定应该使用什么方法等待......不会等待阻止整个方法吗?

FirestoreChangeListener 监听方法

My code is:我的代码是:

  public void Connect()
    {
      // Authentication code...
    
      if (authenticated) CreateFirestoreChangeListeners();
    }
    
    public void CreateFirestoreChangeListeners()
    {
      foreach (var docRef in documentReferences.Values)
      {
        FirestoreChangeListener listener = docRef.Listen(querySnapshot =>
        {
          try
          {
            ProcessSnapshot(querySnapshot);
          }
          catch (Exception ex)
          {
            Debug.WriteLine("FirestoreChangeListener: " + ex);
          }
        });
    
        listener.ListenerTask.ContinueWith((task) =>
        {
          Console.WriteLine("Task faulted");
          var ae = task.Exception;
          if (ae != null)
          {
            ae.Flatten().Handle(ex =>
            {
              Console.WriteLine("Exception Handled");
              return true;
            });
          }
        });
    
        firestoreChangeListeners.Add(listener);
      }
    }

Thanks very much for any assistance anyone can give!非常感谢任何人可以提供的任何帮助!

Kind Regards, Damian亲切的问候,达米安

As per Jon's investigation at https://github.com/googleapis/google-cloud-dotnet/issues/5462 , there is a bug in the underlying gRPC code.根据 Jon 在https://github.com/googleapis/google-cloud-dotnet/issues/5462 上的调查,底层 gRPC 代码中存在一个错误。

Suggested workaround by Jon is also working fine and a good solution to this problem: Jon 建议的解决方法也运行良好,是解决此问题的好方法:

TaskScheduler.UnobservedTaskException += (sender, args) =>
{
    if (args.Exception is AggregateException && args.Exception.InnerException is RpcException)
    {
        args.SetObserved();
    }
};

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM