소스 검색

连接出错时加入重试

Cyrus Zhou 10 달 전
부모
커밋
bbc4bb05a3
1개의 변경된 파일12개의 추가작업 그리고 4개의 파일을 삭제
  1. 12 4
      Admin.NET/Admin.NET.Core/EventBus/EventConsumer.cs

+ 12 - 4
Admin.NET/Admin.NET.Core/EventBus/EventConsumer.cs

@@ -54,13 +54,21 @@ public class EventConsumer<T> : IDisposable
         }
         _consumerCts = new CancellationTokenSource();
         var ct = _consumerCts.Token;
-        _consumerTask = Task.Factory.StartNew(() =>
+        _consumerTask = Task.Factory.StartNew(async () =>
         {
             while (!ct.IsCancellationRequested)
             {
-                var cr = Consumer.TakeOne(10);
-                if (cr == null) continue;
-                Received?.Invoke(this, cr);
+                try
+                {
+                    var cr = Consumer.TakeOne(10);
+                    if (cr == null) continue;
+                    Received?.Invoke(this, cr);
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine($"消息消费异常: {ex.Message}");
+                    await Task.Delay(1000); // 短暂等待后继续尝试
+                }
             }
         }, ct, TaskCreationOptions.LongRunning, TaskScheduler.Default);
     }