1 class Producer 2 { Send() 4 { 5 var factory = new ConnectionFactory() 6 { , 8 Port = 5672, , }; (var connection = factory.CreateConnection()) 14 { 15 using (var channel = connection.CreateModel()) 16 { , type: ); //交换类型 message = Guid.NewGuid().ToString(); 22 var body = Encoding.UTF8.GetBytes(message); , 24 routingKey: "", 25 basicProperties: null, 26 body: body); , message); 29 } ); 32 Console.ReadLine(); 33 } 34 } 35 }
Producer.cs //生产者
1 class Reciver 2 { Recive() 4 { 5 var factory = new ConnectionFactory() 6 { , 8 Port = 5672, , }; (var connection = factory.CreateConnection()) 14 using (var channel = connection.CreateModel()) 15 { , type: ); //交换类型 queueName = channel.QueueDeclare().QueueName; 21 channel.QueueBind(queue: queueName, , 23 routingKey: ""); ); consumer = new EventingBasicConsumer(channel); 28 consumer.Received += (model, ea) => 29 { 30 var body = ea.Body; 31 var message = Encoding.UTF8.GetString(body); , message); 33 }; 34 channel.BasicConsume(queue: queueName, 35 noAck: true, 36 consumer: consumer); ); 39 Console.ReadLine(); 40 } 41 } 42 }
Reciver.cs //接收者尝试发现 - 新物种 EasyNetQ
刚刚接触 RabbitMQ,不会封装?
刚刚入门 RabbitMQ,不敢封装?
刚刚上手 RabbitMQ,不想不懂装懂,怕被别人说你装 13?
这都不是事!EasyNetQ,看名字就知道,搞定 MQ,So easy!
连接 RabbitMQ 代理:
);
发布:
bus.Publish(message);
订阅:
bus.Subscribe<MyMessage>(, msg => Console.WriteLine(msg.Text));
下面我们通过 Demo 来感受一下 Easy 的程度吧,创建项目(效果图如下,附 Demo 下载):
Wen.EasyNetQDemo.Model:类库
Wen.EasyNetQDemo.Publisher,Wen.EasyNetQDemo.Subscriber:控制台应用程序,都使用 Nuget 直接安装 EasyNetQ 包,都引用类库 Model。
Demo.cs
public class Demo { public string Message { get; set; } }
Publisher