private async Task ListenRpc(IConsumer <string, string> consumer, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    var msg            = consumer.Consume(cancellationToken);
                    var replayToHeader = msg.Message.Headers.FirstOrDefault(x => x.Key == "ReplayTo")?.GetValueBytes();
                    if (replayToHeader != null)
                    {
                        if (OnRpcRequest != null)
                        {
                            foreach (var inv in OnRpcRequest.GetInvocationList())
                            {
                                var result = await(Task <string>) inv.DynamicInvoke(msg.Message.Value, msg.Topic);
                                if (result != null)
                                {
                                    await _transportSender.SendAsync(Encoding.UTF8.GetString(replayToHeader), result,
                                                                     false);

                                    break;
                                }
                            }
                        }

                        _logger.LogInformation($"{DateTime.Now} Processed");
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Error while processing rpc request");
                }
            }
        }
Example #2
0
        private Response HandleRpcRequest(RequestReceive eventReceive, string id)
        {
            try
            {
                if (OnRpcRequest != null)
                {
                    foreach (var inv in OnRpcRequest.GetInvocationList())
                    {
                        var result = ((Task <string>)inv
                                      .DynamicInvoke(Encoding.UTF8.GetString(eventReceive.Body),
                                                     eventReceive.Channel))?.ConfigureAwait(false)
                                     .GetAwaiter()
                                     .GetResult();
                        if (result != null)
                        {
                            return(new Response(eventReceive)
                            {
                                Body = Encoding.UTF8.GetBytes(result),
                                CacheHit = false,
                                ClientID = $"{id}{Guid.NewGuid()}",
                                Error = "",
                                Executed = true,
                                Timestamp = DateTime.Now
                            });
                        }
                    }
                }

                return(new Response(eventReceive)
                {
                    Error = "Cant find rpc agent",
                    ClientID = $"{id}{Guid.NewGuid()}",
                    Body = new byte[0]
                });
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Can`t process incoming event");
                return(new Response(eventReceive)
                {
                    Error = e.ToString(),
                    ClientID = $"{id}{Guid.NewGuid()}",
                    Body = new byte[0]
                });
            }
        }