8000 Added dotnet examples (via Jim) · liborange/rabbitmq-tutorials@5b42d5a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b42d5a

Browse files
committed
Added dotnet examples (via Jim)
1 parent f10c746 commit 5b42d5a

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
php/lib/php-amqplib
2+
dotnet/*.exe
3+
dotnet/lib

dotnet/README.md

Lines changed: 29 additions & 0 deletions
< 8000 /div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Dotnet C# code for RabbitMQ tutorials
2+
3+
Here you can find a C# code examples from [RabbitMQ
4+
tutorials](http://www.rabbitmq.com/getstarted.html).
5+
6+
7+
Here we present steps to run the examples using Mono on Linux.
8+
9+
## Requirements
10+
11+
You need Mono and RabbitMQ dotnet client.
12+
13+
sudo apt-get install mono-devel
14+
mkdir lib
15+
cd lib
16+
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1/rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
17+
unzip rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
18+
cd ..
19+
20+
21+
## Code
22+
23+
[Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
24+
25+
gmcs -r:lib/bin/RabbitMQ.Client.dll Send.cs
26+
MONO_PATH=lib/bin mono Send.exe
27+
28+
gmcs -r:lib/bin/RabbitMQ.Client.dll Receive.cs
29+
MONO_PATH=lib/bin mono Receive.exe

dotnet/Receive.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using RabbitMQ.Client;
3+
using RabbitMQ.Client.Events;
4+
5+
namespace Receive {
6+
class Program {
7+
static void Main(string[] args) {
8+
ConnectionFactory factory = new ConnectionFactory();
9+
factory.HostName = "localhost";
10+
IConnection connection = factory.CreateConnection();
11+
IModel channel = connection.CreateModel();
12+
13+
channel.QueueDeclare("test");
14+
15+
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
16+
channel.BasicConsume("test", true, null, consumer);
17+
18+
Console.WriteLine(" [*] Waiting for messages. To exit press CTRL+C");
19+
while(true) {
20+
BasicDeliverEventArgs ea =
21+
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
22+
23+
byte[] body = ea.Body;
24+
string message = System.Text.Encoding.UTF8.GetString(body);
25+
Console.WriteLine(" [x] Received {0}", message);
26+
}
27+
}
28+
}
29+
}

dotnet/Send.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using RabbitMQ.Client;
3+
4+
namespace Send {
5+
class Program {
6+
static void Main(string[] args) {
7+
ConnectionFactory factory = new ConnectionFactory();
8+
factory.HostName = "localhost";
9+
IConnection connection = factory.CreateConnection();
10+
IModel channel = connection.CreateModel();
11+
12+
channel.QueueDeclare("test");
13+
14+
string message = "Hello World!";
15+
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
16+
17+
channel.BasicPublish("", "test", null, body);
18+
Console.WriteLine(" [x] Sent {0}", message);
19+
20+
channel.Close();
21+
connection.Close();
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)
0