Remote Procedure Calls RPC using RabbitMQ in C# .NET

RabbitMQ.NET

RabbitMQ.NET is a .NET Core library to easily integrate RabbitMQ in .NET applications

Nuget
https://www.nuget.org/packages/RabbitMQ.NET/

Github
https://github.com/rizansari/RabbitMqCore

Installation

Install-Package RabbitMQ.NET

Using the library
RabbitMQ.NET is a simple library to Publish and Subscribe easily in .NET Core applications. It now supports Remote Procedure Calls RPC in very intuitive manner

Setup DI

var serviceProvider = new ServiceCollection()
        .AddLogging(loggingBuilder =>
        {
            loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
        })
        .AddRabbitMQCore(options =>
        {
            options.HostName = "localhost";
        })
        .BuildServiceProvider();

Get QueueService

var rmq = serviceProvider.GetRequiredService();

RPC Server

        var rpcServer = rmq.CreateRpcServer(options =>
        {
            options.RpcName = "TEST_RPC";
        });

        rpcServer.Subscribe(request =>
        {
            // get request object
            var obj = JsonConvert.DeserializeObject(request.Message);

            // response with response object
            rpcServer.Respond(new RabbitMessageOutbound() 
            { 
                CorrelationId = request.CorrelationId, 
                Message = JsonConvert.SerializeObject(obj) 
            });
            
        });

RPC Client

        var rpcClient = rmq.CreateRpcClient(options =>
        {
            options.RpcName = "TEST_RPC";
        });

        SimpleObject obj = null;
        RabbitMessageOutbound message = null;

        int count = 1;
        while (true)
        {
            try
            {
                obj = new SimpleObject() { ID = count++, Name = "Request" };
                message = new RabbitMessageOutbound()
                {
                    CorrelationId = $"CorrelationId:{obj.ID}",
                    Message = JsonConvert.SerializeObject(obj)
                };

                rpcClient.Call(message, response => 
                {
                    // response received from RPC Server
                    Console.WriteLine("rpc response: {0}", response.Message);
                }, 10000);
                Console.WriteLine("rpc request: {0}", message.Message);
                Thread.Sleep(2000);
            }
            catch (OutboundMessageFailedException ex)
            {
                Console.WriteLine("Message failed {0}:{1}", obj.ID, ex.RabbitMessageOutbound.Message);
                Thread.Sleep(2000);
            }
            catch (NotConnectedException ex)
            {
                Console.WriteLine("NotConnectedException Message failed {0}", obj.ID);
                Thread.Sleep(2000);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Message failed {0}", obj.ID);
                Thread.Sleep(2000);
            }
        }

        rpcClient.Dispose();

Leave a Reply

Your email address will not be published.