在NuGet引入RabbitMQ.Client
public static void Send() {
ConnectionFactory factory = new ConnectionFactory();
factory.UserName = "guest";
factory.Password = "guest";
factory.HostName = "localhost";
factory.Port = 5672;
using (IConnection conn=factory.CreateConnection())
{
using (IModel channel=conn.CreateModel())
{
channel.QueueDeclare("myqueue",true,false,false,null);
while (true)
{
string message = string.Format("Message_{0}", Console.ReadLine());
Message me = new Message();
me.Url = "https:www.baidu.com";
me.Type = "GET";
me.Body = message;
byte[] buffer = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(me));
IBasicProperties properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
channel.BasicPublish("", "MyRabbitMQ", properties, buffer);
Console.WriteLine("消息发送成功:" + message);
}
}
}
}