RABBITMQ Code First step

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.