Example #1
0
 public void Ping()
 {
     try
     {
         Database.RunCommandAsync((Command <BsonDocument>) "{ping:1}").Wait();
     }
     catch (Exception err)
     {
         DebugText?.Invoke("Failed to ping MongoDB -> " + err.ToString());
     }
 }
        protected virtual void handleReceivedMessage(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var msgObj = Deserialize <EventMessage>(e.Body);
                if (msgObj == null)
                {
                    DebugText?.Invoke(this.GetType().Name + ": Failed to parse received message to RequestMessage");
                    return;
                }

                try
                {
                    if (!msgObj.EventName.Equals(typeof(T).Name))
                    {
                        DebugText?.Invoke(this.GetType().Name + ": Invalid Event type. Expected: " + typeof(T).Name + " Received: " + msgObj.EventName);
                        if (!AutoAck)
                        {
                            channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                        }
                    }

                    var obj = msgObj.GetEvent <T>();
                    Handle(obj, msgObj);
                }
                catch (Exception err)
                {
                    DebugText?.Invoke(this.GetType().Name + ": Failed to Handle Event -> " + err.ToString());

                    if (!AutoAck)
                    {
                        channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                    }
                }

                if (!AutoAck)
                {
                    channel.BasicAck(e.DeliveryTag, false);
                }
            }
            catch (Exception err)
            {
                DebugText.Invoke("RPCSubscription Callback Failed to handle received message -> " + err.ToString());
            }
        }
Example #3
0
        protected virtual void handleReceivedMessage(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var msgObj = JsonConvert.DeserializeObject <RequestMessage>(Encoding.UTF8.GetString(e.Body));
                if (msgObj == null)
                {
                    DebugText?.Invoke(this.GetType().Name + ": Failed to parse received message to RequestMessage");
                    return;
                }

                object result = null;

                try
                {
                    if (!msgObj.RequestName.Equals(typeof(T).Name))
                    {
                        DebugText?.Invoke(this.GetType().Name + ": Invalid Request type. Expected: " + typeof(T).Name + " Received: " + msgObj.RequestName);
                        if (!AutoAck)
                        {
                            channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                        }
                    }

                    var obj = msgObj.GetRequest <T>();
                    result = Handle(obj, msgObj);
                }
                catch (Exception err)
                {
                    DebugText?.Invoke(this.GetType().Name + ": Failed to Handle Request -> " + err.ToString());
                    DebugText?.Invoke(JsonConvert.SerializeObject(msgObj, Formatting.Indented));

                    if (!AutoAck)
                    {
                        channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                    }

                    return;
                }

                if (result == null)
                {
                    if (!AutoAck)
                    {
                        channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                    }

                    DebugText?.Invoke(this.GetType().Name + ": Handler Returned null");
                    return;
                }

                if (!AutoAck)
                {
                    channel.BasicAck(e.DeliveryTag, false);
                }

                if (e.BasicProperties == null || e.BasicProperties.ReplyTo == null)
                {
                    return;
                }

                var responseProps = channel.CreateBasicProperties();
                responseProps.CorrelationId = e.BasicProperties.CorrelationId;
                responseProps.Persistent    = true;

                channel.BasicPublish(
                    exchange: "Response",
                    routingKey: typeof(T).Name + "." + e.BasicProperties.ReplyTo,
                    body: Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(result)),
                    basicProperties: responseProps
                    );
            }
            catch (Exception err)
            {
                DebugText.Invoke("RPCSubscription Callback Failed to handle received message -> " + err.ToString());
            }
        }
        private bool check_connection()
        {
            lock (connectionLock)
            {
                if (connection != null && connection.IsOpen)
                {
                    return(true);
                }
                else if (connection != null && !connection.IsOpen)
                {
                    return(false);
                }

                try
                {
                    if (connection_factory == null)
                    {
                        try
                        {
                            connection_factory = new ConnectionFactory
                            {
                                UserName                = options.Username,
                                Password                = options.Password,
                                HostName                = options.Hostname,
                                VirtualHost             = options.VirtualHost,
                                NetworkRecoveryInterval = TimeSpan.FromSeconds(5),
                                Port = options.Port,
                            };

                            if (options.UseSSL)
                            {
                                connection_factory.Ssl = new SslOption()
                                {
                                    Version = System.Security.Authentication.SslProtocols.Tls12,
                                    Enabled = true,
                                    AcceptablePolicyErrors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors | System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch
                                };
                            }
                        }
                        catch (Exception err)
                        {
                            return(false);
                        }
                    }



                    if (connection == null)
                    {
                        try
                        {
                            connection = connection_factory.CreateConnection();

                            if (connection != null)
                            {
                                connection.ConnectionShutdown += (s, e) =>
                                {
                                    DebugText?.Invoke("RabbitMQ Connection Shut Down -> " + e.ReplyText);
                                };

                                connection.RecoverySucceeded += (s, e) =>
                                {
                                    DebugText?.Invoke("RabbitMQ Connetion Successfully Recovered");
                                };

                                connection.ConnectionRecoveryError += (s, e) =>
                                {
                                    DebugText?.Invoke("RabbitMQ Connection Failed to Recover");
                                };
                            }


                            DebugText?.Invoke("RabbitMQ Connected to " + connection_factory.HostName + " on port " + connection_factory.Port);
                        }
                        catch (Exception err)
                        {
                            return(false);
                        }
                    }
                }
                catch (Exception err)
                {
                    return(false);
                }
            }

            return(true);
        }
Example #5
0
 public void AddSubscription(ISubscription subscription)
 {
     subscription.Subscribe(connection, stopSubscriptionsTokenSource.Token, publisher, client);
     subscription.DebugText += (txt) => DebugText?.Invoke(txt);
 }