8000 Updated dotnet code. · syk-coder/rabbitmq-tutorials@f2c3fb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2c3fb0

Browse files
committed
Updated dotnet code.
1 parent 26422be commit f2c3fb0

File tree

7 files changed

+173
-48
lines changed

7 files changed

+173
-48
lines changed

dotnet/EmitLog.cs

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

dotnet/NewTask.cs

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

dotnet/README.md

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,88 @@
33
Here you can find C# code examples for [RabbitMQ
44
tutorials](http://www.rabbitmq.com/getstarted.html).
55

6-
You'll need erlang installed, and also access to a [RabbitMQ server](http://www.rabbitmq.com/server.html).
7-
These are easy to [install](http://www.rabbitmq.com/install.html).
6+
## Requirements
87

8+
### Requirements on Windows
99

10-
## Requirements
10+
You need the RabbitMQ dotnet client.
11+
12+
* Download [rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip](http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.4.1/rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip)
13+
* Extract it and copy "RabbitMQ.Client.dll" to your working folder.
14+
15+
You also need to nsure your system can find the C# compiler `csc.exe`,
16+
you may need to add `;C:\WINDOWS\Microsoft.NET\Framework\v3.5` to your
17+
Path.
18+
19+
We're suggesting to use the command line (start->run cmd.exe) to
20+
compile and run the code. Alternatively you could use Visual Studio, but
21+
due to the nature of examples command line is a preferred interface.
22+
23+
### Requirements on Linux
1124

12-
### Mono on Linux
1325
You need Mono and RabbitMQ dotnet client.
1426

1527
sudo apt-get install mono-devel
1628
mkdir lib
1729
cd lib
18-
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1/rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
19-
unzip rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
30+
wget http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.4.1/rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip
31+
unzip rabbitmq-dotnet-client-2.4.1-dotnet-3.0.zip
2032
cd ..
2133

22-
23-
### Windows
24-
You need the RabbitMQ dotnet client.
25-
26-
Go to http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.1.1
27-
Download rabbitmq-dotnet-client-2.1.1-dotnet-3.0.zip
28-
Extract it to rabbitmq-dotnet-client-2.1.1-dotnet-3.0 in your working folder
2934

30-
3135
## Code
3236

33-
For background, you can refer to [Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html):
37+
#### [Tutorial one: "Hello World!"](http://www.rabbitmq.com/tutorial-one-python.html)
3438

39+
##### Windows
3540

36-
### Compile and run the C# examples using Mono on Linux.
41+
csc /r:"RabbitMQ.Client.dll" Send.cs
42+
csc /r:"RabbitMQ.Client.dll" Receive.cs
3743

38-
gmcs -r:lib/bin/RabbitMQ.Client.dll Send.cs
39-
MONO_PATH=lib/bin mono Send.exe
44+
Send.exe
45+
Receive.exe
4046

47+
##### Linux
48+
49+
gmcs -r:lib/bin/RabbitMQ.Client.dll Send.cs
4150
gmcs -r:lib/bin/RabbitMQ.Client.dll Receive.cs
51+
52+
MONO_PATH=lib/bin mono Send.exe
4253
MONO_PATH=lib/bin mono Receive.exe
43-
44-
45-
### Compile the C# examples on Windows
4654

47-
Ensure your system can find the c# compiler `csc.exe`
4855

49-
e.g. Add `;C:\WINDOWS\Microsoft.NET\Framework\v3.5` to your Path
50-
51-
If you put the whole client directory in your working directory:
56+
#### [Tutorial two: Work Queues](http://www.rabbitmq.com/tutorial-two-python.html):
5257

53-
csc /r:".\rabbitmq-dotnet-client-2.1.1-dotnet-3.0\bin\RabbitMQ.Client.dll" Send.cs
54-
csc /r:".\rabbitmq-dotnet-client-2.1.1-dotnet-3.0\bin\RabbitMQ.Client.dll" Receive.cs
58+
##### Windows
5559

56-
or, if you just copy the RabbitMQ.Client.dll client library to your working directory:
60+
csc /r:"RabbitMQ.Client.dll" NewTask.cs
61+
csc /r:"RabbitMQ.Client.dll" Worker.cs
5762

58-
csc /r:"RabbitMQ.Client.dll" Send.cs
59-
csc /r:"RabbitMQ.Client.dll" Receive.cs
63+
NewTask.exe
64+
Worker.exe
6065

61-
or you could use MS Visual Studio.
66+
##### Linux
6267

68+
gmcs -r:lib/bin/RabbitMQ.Client.dll NewTask.cs
69+
gmcs -r:lib/bin/RabbitMQ.Client.dll Worker.cs
6370

64-
### Run the example programs on Windows
71+
MONO_PATH=lib/bin mono NewTask.exe
72+
MONO_PATH=lib/bin mono Worker.exe
6573

66-
Open 3 Command Prompt windows Start > Run... cmd
74+
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html)
6775

68-
Use `rabbitmqctl status` to check the server is running,
69-
and `rabbitmqctl list_queues` to inspect the queue.
76+
##### Windows
7077

71-
In the other two windows, navigate to your working directory to run the example client programs.
78+
csc /r:"RabbitMQ.Client.dll" ReceiveLogs.cs
79+
csc /r:"RabbitMQ.Client.dll" EmitLog.cs
7280

73-
In another cmd window, send a message:
74-
75-
Send.exe
81+
ReceiveLogs.exe
82+
EmitLog.exe
7683

77-
Check queue identified as "hello" has 1 message.
78-
In the final cmd window, set the listener going:
84+
##### Linux
7985

80-
Receive.exe
86+
gmcs -r:lib/bin/RabbitMQ.Client.dll ReceiveLogs.cs
87+
gmcs -r:lib/bin/RabbitMQ.Client.dll EmitLog.cs
8188

82-
This will keep listening (Ctrl-C in this window will stop it) for messages.
83-
You should now see the first message, and the queue should be empty.
84-
The Receive view should get any further messages you Send.
89+
MONO_PATH=lib/bin mono ReceiveLogs.exe
90+
MONO_PATH=lib/bin mono EmitLog.exe

dotnet/Receive.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ static void Main(string[] args) {
99
IConnection connection = factory.CreateConnection();
1010
IModel channel = connection.CreateModel();
1111

12-
channel.QueueDeclare("hello");
12+
channel.QueueDeclare("hello", false, false, false, null);
1313

1414
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
15-
channel.BasicConsume("hello", true, null, consumer);
15+
channel.BasicConsume("hello", true, consumer);
1616

1717
System.Console.WriteLine(" [*] Waiting for messages." +
1818
"To exit press CTRL+C");

dotnet/ReceiveLogs.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using RabbitMQ.Client;
2+
using RabbitMQ.Client.Events;
3+
4+
namespace ReceiveLogs {
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.ExchangeDeclare("logs", "fanout");
13+
14+
string queue_name = channel.QueueDeclare();
15+
16+
channel.QueueBind(queue_name, "logs", "");
17+
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
18+
channel.BasicConsume(queue_name, true, consumer);
19+
20+
System.Console.WriteLine(" [*] Waiting for logs." +
21+
"To exit press CTRL+C");
22+
while(true) {
23+
BasicDeliverEventArgs ea =
24+
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
25+
26+
byte[] body = ea.Body;
27+
string message = System.Text.Encoding.UTF8.GetString(body);
28+
System.Console.WriteLine(" [x] {0}", message);
29+
}
30+
}
31+
}
32+
}

dotnet/Send.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static void Main(string[] args) {
88
IConnection connection = factory.CreateConnection();
99
IModel channel = connection.CreateModel();
1010

11-
channel.QueueDeclare("hello");
11+
channel.QueueDeclare("hello", false, false, false, null);
1212

1313
string message = "Hello World!";
1414
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);

dotnet/Worker.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using RabbitMQ.Client;
2+
using RabbitMQ.Client.Events;
3+
4+
namespace Worker {
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("task_queue", true, false, false, null);
13+
14+
channel.BasicQos(0, 1, false);
15+
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
16+
channel.BasicConsume("task_queue", false, consumer);
17+
18+
System.Console.WriteLine(" [*] Waiting for messages." +
19+
"To exit press CTRL+C");
20+
while(true) {
21+
BasicDeliverEventArgs ea =
22+
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
23+
24+
byte[] body = ea.Body;
25+
string message = System.Text.Encoding.UTF8.GetString(body);
26+
System.Console.WriteLine(" [x] Received {0}", message);
27+
28+
int dots = message.Split('.').Length - 1;
29+
System.Threading.Thread.Sleep(dots * 1000);
30+
31+
System.Console.WriteLine(" [x] Done");
32+
33+
channel.BasicAck(ea.DeliveryTag, false);
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)
0