Hello RabbitMQ
Finally to the use. NET connection RabbitMQ, we start by creating a new console application that downloads RabbitMQ in NuGet in the package management controller.
Install-package rabbitmq.client
Once the installation is complete, you can start a RABBITMQ message publishing program. First using Rabbitmq.client;
From the factory to get the instance, the user here does not recommend the use of guest, because guest is set the loopback, that is, only local can be logged in, the other machine in the browser is not able to use the Guest account login, of course, can be set in config, but it is no good
// get the instance local host, user admin from the factory var New connectionfactory () { "admin", "admin " , "localhost" };
Some friends will say, I do not have an account? You can use the CTL from the last blog post to create a user
Rabbitmqctl Add_user Admin Admin
Then set vhost permissions for the Admin user
' .* ' ' .* ' ' .* '
Back in the code, create a new connection, because it needs to be freed after the connection is used, so we can use the using package.
// Create a connection using (var connection = factory.) CreateConnection ())
With the connection, you can create a CHANNL (channel).
// Create a new channel to return using (var channel = connection.) Createmodel ())
The next step is to specify a switch, but the RABBITMQ has a default switch for convenience so this step ignores
Declares a queue and then publishes a message, which allows the project to run.
//declaring queuesChannel. Queuedeclare ("firsttest",true,false,false,NULL);//Publish a messagevarmsg = Encoding.UTF8.GetBytes ("Hello RabbitMQ"); channel. Basicpublish (string. Empty, Routingkey:"firsttest", Basicproperties:NULL, body:msg);
In the Web management tool you can see our new queue firsttest, and there is a message
At this time can write a consumer (consumer) client to read messages in the queue
Consumer clients do not need to publish and create queues, which is consistent except for declaring queues and publishing messages. So we'll start by returning to a new channel.
The Basicget method return type is Basicgetresult, which has some basic information, such as how many messages are in the queue, the current switch type, and, of course, our most important message Body
// Get Message var result = Channel. Basicget ("firsttest"true);
var msg = Encoding.UTF8.GetString (result. Body);
Print messages
Console.WriteLine (msg);
RABBITMQ Code First step