2
2
using RabbitMQ . Client ;
3
3
using RabbitMQ . Client . Events ;
4
4
5
- class RPCClient {
6
- private static string RpcCall ( string message ) {
7
- string response = null ;
5
+ class RPCClient : IDisposable {
6
+ private IConnection connection ;
7
+ private IModel channel ;
8
+ private string replyQueueName ;
9
+ private QueueingBasicConsumer consumer ;
10
+
11
+ public RPCClient ( ) {
8
12
ConnectionFactory factory = new ConnectionFactory ( ) ;
9
13
factory . HostName = "localhost" ;
10
- using ( IConnection connection = factory . CreateConnection ( ) )
11
- using ( IModel channel = connection . CreateModel ( ) ) {
12
- string replyQueueName = channel . QueueDeclare ( ) ;
13
- QueueingBasicConsumer consumer = new QueueingBasicConsumer ( channel ) ;
14
- channel . BasicConsume ( replyQueueName , false , consumer ) ;
14
+ connection = factory . CreateConnection ( ) ;
15
+ channel = connection . CreateModel ( ) ;
16
+ replyQueueName = channel . QueueDeclare ( ) ;
17
+ consumer = new QueueingBasicConsumer ( channel ) ;
18
+ channel . BasicConsume ( replyQueueName , false , consumer ) ;
19
+ }
15
20
16
- string corrId = Guid . NewGuid ( ) . ToString ( ) ;
17
- IBasicProperties props = channel . CreateBasicProperties ( ) ;
18
- props . ReplyTo = replyQueueName ;
19
- props . CorrelationId = corrId ;
21
+ public string Call ( string message ) {
22
+ string response = null ;
23
+ string corrId = Guid . NewGuid ( ) . ToString ( ) ;
24
+ IBasicProperties props = channel . CreateBasicProperties ( ) ;
25
+ props . ReplyTo = replyQueueName ;
26
+ props . CorrelationId = corrId ;
20
27
21
- byte [ ] messageBytes = System . Text . Encoding . UTF8 . GetBytes ( message ) ;
22
- channel . BasicPublish ( "" , "rpc_queue" , props , messageBytes ) ;
28
+ byte [ ] messageBytes = System . Text . Encoding . UTF8 . GetBytes ( message ) ;
29
+ channel . BasicPublish ( "" , "rpc_queue" , props , messageBytes ) ;
23
30
24
- while ( true ) {
25
- BasicDeliverEventArgs ea = ( BasicDeliverEventArgs ) consumer . Queue . Dequeue ( ) ;
26
- if ( ea . BasicProperties . CorrelationId == corrId ) {
27
- byte [ ] body = ea . Body ;
28
- response = System . Text . Encoding . UTF8 . GetString ( body ) ;
29
- channel . BasicCancel ( consumer . ConsumerTag ) ;
30
- break ;
31
- }
31
+ while ( true ) {
32
+ BasicDeliverEventArgs ea =
33
+ ( BasicDeliverEventArgs ) consumer . Queue . Dequeue ( ) ;
34
+ if ( ea . BasicProperties . CorrelationId == corrId ) {
35
+ byte [ ] body = ea . Body ;
36
+ response = System . Text . Encoding . UTF8 . GetString ( body ) ;
37
+ channel . BasicCancel ( consumer . ConsumerTag ) ;
38
+ break ;
32
39
}
33
- return response ;
34
40
}
41
+ return response ;
42
+ }
43
+ public void Dispose ( ) {
44
+ connection . Close ( ) ;
35
45
}
36
46
37
47
public static void Main ( ) {
38
48
Console . WriteLine ( " [x] Requesting fib(30)" ) ;
39
- string response = RpcCall ( "30" ) ;
40
- Console . WriteLine ( " [.] Got '{0}'" , response ) ;
49
+ using ( RPCClient rpcClient = new RPCClient ( ) ) {
50
+ string response = rpcClient . Call ( "30" ) ;
51
+ Console . WriteLine ( " [.] Got '{0}'" , response ) ;
52
+ }
41
53
}
42
- }
54
+ }
0 commit comments