From 511c3080a61b6421d3fe5ac28b497e8129a86e6a Mon Sep 17 00:00:00 2001 From: Noe Date: Mon, 6 Nov 2023 14:29:04 +0100 Subject: [PATCH] Put try-catch statement when trying to connect to the queue for the first time --- csharp/App/SaliMax/src/Program.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/csharp/App/SaliMax/src/Program.cs b/csharp/App/SaliMax/src/Program.cs index 32e391b31..245281a1f 100644 --- a/csharp/App/SaliMax/src/Program.cs +++ b/csharp/App/SaliMax/src/Program.cs @@ -298,14 +298,21 @@ internal static class Program private static void SubscribeToQueue(SalimaxAlarmState currentSalimaxState, String? s3Bucket) { - _factory = new ConnectionFactory { HostName = VpnServerIp }; - _connection = _factory.CreateConnection(); - _channel = _connection.CreateModel(); - _channel.QueueDeclare(queue: "statusQueue", durable: false, exclusive: false, autoDelete: false, arguments: null); - Console.WriteLine("The controller sends its status to the middleware for the first time"); - if (s3Bucket != null) InformMiddleware(s3Bucket, (Int32)currentSalimaxState); - - _subscribedToQueue = true; + try + { + _factory = new ConnectionFactory { HostName = VpnServerIp }; + _connection = _factory.CreateConnection(); + _channel = _connection.CreateModel(); + _channel.QueueDeclare(queue: "statusQueue", durable: false, exclusive: false, autoDelete: false, arguments: null); + Console.WriteLine("The controller sends its status to the middleware for the first time"); + if (s3Bucket != null) InformMiddleware(s3Bucket, (Int32)currentSalimaxState); + + _subscribedToQueue = true; + } + catch (Exception ex) + { + Console.WriteLine("An error occurred while connecting to the RabbitMQ queue: " + ex.Message); + } } private static IPAddress FindVpnIp()