Skip to content

Commit 7e457af

Browse files
Merge pull request #148 from darrylsk/master
Replace deprecated QueueingBasicConsumer in csharp code for tutorial six
2 parents bfc62e1 + c718cad commit 7e457af

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

dotnet/RPCClient/RPCClient.cs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
2+
using System.Collections.Concurrent;
43
using System.Text;
5-
using System.Threading.Tasks;
64
using RabbitMQ.Client;
75
using RabbitMQ.Client.Events;
86

9-
class RPCClient
7+
public class RpcClient
108
{
11-
private IConnection connection;
12-
private IModel channel;
13-
private string replyQueueName;
14-
private QueueingBasicConsumer consumer;
9+
private readonly IConnection connection;
10+
private readonly IModel channel;
11+
private readonly string replyQueueName;
12+
private readonly EventingBasicConsumer consumer;
13+
private readonly BlockingCollection<string> respQueue = new BlockingCollection<string>();
14+
private readonly IBasicProperties props;
1515

16-
public RPCClient()
16+
public RpcClient()
1717
{
1818
var factory = new ConnectionFactory() { HostName = "localhost" };
19+
1920
connection = factory.CreateConnection();
2021
channel = connection.CreateModel();
2122
replyQueueName = channel.QueueDeclare().QueueName;
22-
consumer = new QueueingBasicConsumer(channel);
23-
channel.BasicConsume(queue: replyQueueName, autoAck: true, consumer: consumer);
23+
consumer = new EventingBasicConsumer(channel);
24+
25+
props = channel.CreateBasicProperties();
26+
var correlationId = Guid.NewGuid().ToString();
27+
props.CorrelationId = correlationId;
28+
props.ReplyTo = replyQueueName;
29+
30+
consumer.Received += (model, ea) =>
31+
{
32+
var body = ea.Body;
33+
var response = Encoding.UTF8.GetString(body);
34+
if (ea.BasicProperties.CorrelationId == correlationId)
35+
{
36+
respQueue.Add(response);
37+
}
38+
};
2439
}
2540

2641
public string Call(string message)
2742
{
28-
var corrId = Guid.NewGuid().ToString();
29-
var props = channel.CreateBasicProperties();
30-
props.ReplyTo = replyQueueName;
31-
props.CorrelationId = corrId;
3243

3344
var messageBytes = Encoding.UTF8.GetBytes(message);
34-
channel.BasicPublish(exchange: "", routingKey: "rpc_queue", basicProperties: props, body: messageBytes);
45+
channel.BasicPublish(
46+
exchange: "",
47+
routingKey: "rpc_queue",
48+
basicProperties: props,
49+
body: messageBytes);
3550

36-
while(true)
37-
{
38-
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
39-
if(ea.BasicProperties.CorrelationId == corrId)
40-
{
41-
return Encoding.UTF8.GetString(ea.Body);
42-
}
43-
}
51+
52+
channel.BasicConsume(
53+
consumer: consumer,
54+
queue: replyQueueName,
55+
autoAck: true);
56+
57+
return respQueue.Take(); ;
4458
}
4559

4660
public void Close()
@@ -49,16 +63,17 @@ public void Close()
4963
}
5064
}
5165

52-
class RPC
66+
public class Rpc
5367
{
5468
public static void Main()
5569
{
56-
var rpcClient = new RPCClient();
70+
var rpcClient = new RpcClient();
5771

5872
Console.WriteLine(" [x] Requesting fib(30)");
5973
var response = rpcClient.Call("30");
60-
Console.WriteLine(" [.] Got '{0}'", response);
6174

75+
Console.WriteLine(" [.] Got '{0}'", response);
6276
rpcClient.Close();
6377
}
6478
}
79+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy