Ejemplo n.º 1
0
 /// <summary>
 /// Register a new account in database
 /// </summary>
 /// <param name="username">User name</param>
 /// <param name="email">Email</param>
 /// <param name="password">Password</param>
 /// <returns>returns true if user added to database</returns>
 public bool Register(string email, string username, string password)
 {
     Core.Data.User user = new Core.Data.User();
     user.email    = email;
     user.username = username;
     user.password = password;
     Core.Data.DataProvider db = Core.Data.DataProvider.DataProviderFactory();
     return(db.Insert(user));
 }
Ejemplo n.º 2
0
        public void Publish <T>(T message, string exchangeName, string exchangeType, string routeKey)
            where T : class
        {
            if (message == null)
            {
                return;
            }

            var channel = _objectPool.Get();

            try
            {
                channel.ExchangeDeclare(exchangeName, exchangeType, true, false, null);

                var sendBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
                // var SerializeObject = new System.Xml.Serialization.XmlSerializer(typeof(GeoPoints));
                // var sendBytes = Encoding.UTF8.GetBytes(Serialize(message));

                var properties = channel.CreateBasicProperties();
                properties.Persistent = true;
                properties.MessageId  = Guid.NewGuid().ToString();
                var typeName = typeof(T).FullName;
                properties.Headers = new Dictionary <string, object> {
                    { "NServiceBus.EnclosedMessageTypes", typeName }
                };
                var correlationId = Guid.NewGuid().ToString();
                properties.CorrelationId = correlationId;
                string replyQueueName = "RabbitMQ";//channel.QueueDeclare().QueueName;
                properties.ReplyTo = replyQueueName;

                EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body     = ea.Body;
                    var response = Encoding.UTF8.GetString(body);
                    var msg      = JsonConvert.DeserializeObject <GeoPoints>(response.Substring(1));
                    if (msg != null)
                    {
                        Core.Data.DataProvider db = Core.Data.DataProvider.DataProviderFactory();
                        // Insert into database
                        bool res = db.Insert(new Core.Data.GeoData()
                        {
                            Distance    = msg.Distance,
                            StartingLat = msg.StartingLat,
                            StartingLng = msg.StartingLng,
                            EndingLat   = msg.EndingLat,
                            EndingLng   = msg.EndingLng,
                            UserGUID    = msg.UserGUID
                        });
                    }
                    ((EventingBasicConsumer)model).Model.BasicAck(ea.DeliveryTag, false);
                };

                channel.BasicConsume(
                    consumer: consumer,
                    queue: replyQueueName,
                    autoAck: false);

                channel.BasicPublish(exchangeName, routeKey, properties, sendBytes);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                _objectPool.Return(channel);
            }
        }