Skip to content

Commit 2049e4c

Browse files
committed
Rewrite .NET RPC Server to use EventingBasicConsumer
1 parent 5f4af53 commit 2049e4c

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

dotnet-visual-studio/6_RPCServer/Program.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ class Program
88
public static void Main()
99
{
1010
var factory = new ConnectionFactory() { HostName = "localhost" };
11-
using(var connection = factory.CreateConnection())
12-
using(var channel = connection.CreateModel())
11+
using (var connection = factory.CreateConnection())
12+
using (var channel = connection.CreateModel())
1313
{
1414
channel.QueueDeclare(queue: "rpc_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
1515
channel.BasicQos(0, 1, false);
16-
var consumer = new QueueingBasicConsumer(channel);
16+
var consumer = new EventingBasicConsumer(channel);
1717
channel.BasicConsume(queue: "rpc_queue", noAck: false, consumer: consumer);
1818
Console.WriteLine(" [x] Awaiting RPC requests");
1919

20-
while(true)
20+
consumer.Received += (model, ea) =>
2121
{
2222
string response = null;
23-
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
2423

2524
var body = ea.Body;
2625
var props = ea.BasicProperties;
@@ -34,7 +33,7 @@ public static void Main()
3433
Console.WriteLine(" [.] fib({0})", message);
3534
response = fib(n).ToString();
3635
}
37-
catch(Exception e)
36+
catch (Exception e)
3837
{
3938
Console.WriteLine(" [.] " + e.Message);
4039
response = "";
@@ -45,7 +44,10 @@ public static void Main()
4544
channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes);
4645
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
4746
}
48-
}
47+
};
48+
49+
Console.WriteLine(" Press [enter] to exit.");
50+
Console.ReadLine();
4951
}
5052
}
5153

@@ -55,7 +57,7 @@ public static void Main()
5557
/// </summary>
5658
private static int fib(int n)
5759
{
58-
if(n == 0 || n == 1)
60+
if (n == 0 || n == 1)
5961
{
6062
return n;
6163
}

dotnet/RPCServer.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ class RPCServer
88
public static void Main()
99
{
1010
var factory = new ConnectionFactory() { HostName = "localhost" };
11-
using(var connection = factory.CreateConnection())
12-
using(var channel = connection.CreateModel())
11+
using (var connection = factory.CreateConnection())
12+
using (var channel = connection.CreateModel())
1313
{
1414
channel.QueueDeclare(queue: "rpc_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
1515
channel.BasicQos(0, 1, false);
16-
var consumer = new QueueingBasicConsumer(channel);
16+
var consumer = new EventingBasicConsumer(channel);
1717
channel.BasicConsume(queue: "rpc_queue", noAck: false, consumer: consumer);
1818
Console.WriteLine(" [x] Awaiting RPC requests");
1919

20-
while(true)
20+
consumer.Received += (model, ea) =>
2121
{
2222
string response = null;
23-
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
2423

2524
var body = ea.Body;
2625
var props = ea.BasicProperties;
@@ -34,7 +33,7 @@ public static void Main()
3433
Console.WriteLine(" [.] fib({0})", message);
3534
response = fib(n).ToString();
3635
}
37-
catch(Exception e)
36+
catch (Exception e)
3837
{
3938
Console.WriteLine(" [.] " + e.Message);
4039
response = "";
@@ -45,7 +44,10 @@ public static void Main()
4544
channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes);
4645
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
4746
}
48-
}
47+
};
48+
49+
Console.WriteLine(" Press [enter] to exit.");
50+
Console.ReadLine();
4951
}
5052
}
5153

@@ -55,7 +57,7 @@ public static void Main()
5557
/// </summary>
5658
private static int fib(int n)
5759
{
58-
if(n == 0 || n == 1)
60+
if (n == 0 || n == 1)
5961
{
6062
return n;
6163
}

0 commit comments

Comments
 (0)