Exemplo n.º 1
0
        public async Task SendMessageAsync <T>(BrokerConnection connection, T command) where T : class
        {
            var jsonContent = JsonConvert.SerializeObject(command);

            var client  = QueueClient.CreateFromConnectionString(connection.EndPoint, connection.QueueName);
            var message = new BrokeredMessage(jsonContent);

            await client.SendAsync(message);
        }
Exemplo n.º 2
0
        public async Task ReceiveMessageAsync(BrokerConnection connection, Action <string> actionEvent)
        {
            var client = QueueClient.CreateFromConnectionString(connection.EndPoint, connection.QueueName);

            client.OnMessage(message =>
            {
                var value = new StreamReader(message.GetBody <Stream>(), Encoding.UTF8).ReadToEnd();
                actionEvent(value);

                Console.WriteLine(String.Format("Message body: {0}", value));
                Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
            });
        }
Exemplo n.º 3
0
        public async Task SendMessageAsync <T>(BrokerConnection connection, T command) where T : class
        {
            var message = JsonConvert.SerializeObject(command);

            var factory = new ConnectionFactory()
            {
                HostName = connection.EndPoint,
                Port     = connection.Port,
                UserName = connection.UserName,
                Password = connection.Password
            };

            using (var factoryCon = factory.CreateConnection())
                using (var channel = factoryCon.CreateModel())
                {
                    channel.ExchangeDeclare(
                        exchange: "blissmodirectexchange",
                        type: "direct",
                        durable: false,
                        autoDelete: false,
                        arguments: null
                        );

                    channel.QueueDeclare(
                        queue: connection.QueueName,
                        durable: false,
                        exclusive: false,
                        autoDelete: false,
                        arguments: null
                        );

                    channel.QueueBind(connection.QueueName, "blissmodirectexchange", connection.QueueName);

                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(
                        exchange: "blissmodirectexchange",
                        routingKey: connection.QueueName,
                        basicProperties: null,
                        body: body);
                }
        }
Exemplo n.º 4
0
        public async Task ReceiveMessageAsync(BrokerConnection connection, Action <string> actionEvent)
        {
            var factory = new ConnectionFactory()
            {
                HostName = connection.EndPoint,
                Port     = connection.Port,
                UserName = connection.UserName,
                Password = connection.Password
            };

            this._connection = factory.CreateConnection();
            this._channel    = _connection.CreateModel();

            _channel.QueueBind(
                queue: connection.QueueName,
                exchange: "blissmodirectexchange",
                routingKey: connection.QueueName,
                arguments: null);

            var consumer = new EventingBasicConsumer(_channel);

            consumer.Received += (model, ea) =>
            {
                var body    = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                actionEvent(message);
            };

            consumer.ConsumerCancelled += (sender, ea) => Dispose();

            _channel.BasicConsume(
                queue: connection.QueueName,
                autoAck: true,
                consumer: consumer
                );
        }