예제 #1
0
        private static void SendErrorMessage(Exception ex)
        {
            CustomError error = new CustomError();

            error.application_name = "frontend";
            error.timestamp        = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fff");
            error.message          = ex.ToString();

            //Make an XML from the error object
            XmlSerializer           xmlSerializer = new XmlSerializer(typeof(CustomError));
            XmlSerializerNamespaces ns            = new XmlSerializerNamespaces();

            ns.Add("", "");

            string xml;
            var    settings = new XmlWriterSettings {
                Encoding = Encoding.UTF8, Indent = true
            };
            var stringBuilder = new StringBuilder();

            using (var sww = new ExtendedStringWriter(stringBuilder, Encoding.UTF8))
            {
                using (XmlWriter writer = XmlWriter.Create(sww, settings))
                {
                    xmlSerializer.Serialize(writer, error, ns);
                    xml = sww.ToString();
                }
            }

            //XML validation with XSD
            var xmlValidationResponse = XsdValidation.XmlStringValidation(xml);

            if (xmlValidationResponse != null)
            {
                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        var addUserBody = Encoding.UTF8.GetBytes(xml);
                        channel.BasicPublish(exchange: "logs.exchange",
                                             routingKey: "",
                                             body: addUserBody
                                             );
                    }
            }
        }
예제 #2
0
        private static void ReceiverRabbitMQ()
        {
            try
            {
                //Make the connection to receive
                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += async(model, ea) =>
                        {
                            //Receiving data
                            var body = ea.Body.ToArray();
                            var xml  = Encoding.UTF8.GetString(body);
                            Console.WriteLine(" [x] Received {0}", xml);

                            var xmlValidationResponse = XsdValidation.XmlStringValidation(xml);

                            //When no errors in validation make an object out of the xml data
                            if (xmlValidationResponse != null)
                            {
                                if (xmlValidationResponse == "add_user")
                                {
                                    await ReceivingNewUserAsync(xml);
                                }
                                else if (xmlValidationResponse == "patch_user")
                                {
                                    await ReceivingPatchUserAsync(xml);
                                }
                            }
                        };
                        channel.BasicConsume(queue: "frontend.queue",
                                             autoAck: true,
                                             consumer: consumer);

                        Console.WriteLine(" Press [enter] to exit.");
                        Console.ReadLine();
                    }
            }
            catch (Exception ex) {
                SendErrorMessage(ex);
            }
        }
예제 #3
0
        private static void SendLogToLogExchange(string action)
        {
            //make log file entity
            Log log = new Log("Frontend " + action);

            //Make an XML from the object
            XmlSerializer           xmlSerializer = new XmlSerializer(log.GetType());
            XmlSerializerNamespaces ns            = new XmlSerializerNamespaces();

            ns.Add("", "");

            string xml;
            var    settings = new XmlWriterSettings {
                Encoding = Encoding.UTF8, Indent = true
            };
            var stringBuilder = new StringBuilder();

            using (var sww = new ExtendedStringWriter(stringBuilder, Encoding.UTF8))
            {
                using (XmlWriter writer = XmlWriter.Create(sww, settings))
                {
                    xmlSerializer.Serialize(writer, log, ns);
                    xml = sww.ToString();
                }
            }

            //Validate XML
            var xmlResponse = XsdValidation.XmlStringValidation(xml);

            //when no errors send the message to rabbitmq
            if (xmlResponse != null)
            {
                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        var addUserBody = Encoding.UTF8.GetBytes(xml);
                        channel.BasicPublish(exchange: "logs.exchange",
                                             routingKey: "",
                                             body: addUserBody
                                             );
                    }
            }
        }