public void RpcListen <THandler>(string queueName) { var rpcQueueName = RpcQueueName(queueName); _connection.sendChannel.QueueDeclare(rpcQueueName, false, false, false, null); var consumer = new EventingBasicConsumer(_connection.recieveChannel); _connection.recieveChannel.BasicConsume(rpcQueueName, false, "", false, true, null, consumer); consumer.Received += async(model, ea) => { var handlerResponse = await _handlerFactory.Handle <THandler>(ea.Body); if (handlerResponse is OkResponseGeneric <RpcMessage> ) { var hResponse = handlerResponse as OkResponseGeneric <RpcMessage>; var message = new RpcMessage(hResponse.Data.Data); var responseMessage = RpcMessages.ToByte(message); var properties = _connection.sendChannel.CreateBasicProperties(); properties.ContentType = "application/json"; _connection.sendChannel.BasicPublish("", hResponse.Data.ReturnQueue, false, properties, responseMessage); _connection.recieveChannel.BasicAck(ea.DeliveryTag, false); } else { //LOG ERROR } }; }
public async Task <RpcMessage> RpcExec(string queueName, string message) { var taskCompletion = new TaskCompletionSource <RpcMessage>(); var id = Guid.NewGuid().ToString(); var returnQueue = ReturnQueueName(queueName, id); _connection.recieveChannel.QueueDeclare(returnQueue, false, false, true, null); var consumer = new EventingBasicConsumer(_connection.recieveChannel); _connection.recieveChannel.BasicConsume(returnQueue, false, "", false, true, null, consumer); var expiration = UnixTime.To(DateTime.Now.AddSeconds(TimeoutSeconds)); var request = new RpcRequestMessage(message, returnQueue, expiration); var r = RpcMessages.ToByte(request); Thread.Sleep(TimeSpan.FromTicks(1)); var rpcQueueName = RpcQueueName(queueName); var properties = _connection.sendChannel.CreateBasicProperties(); properties.ContentType = "application/json"; properties.Expiration = expiration.ToString(); _connection.sendChannel.BasicPublish("", rpcQueueName, false, properties, r); consumer.Received += (model, ea) => { _connection.recieveChannel.QueueDelete(returnQueue, false, true); if (DateTime.Now > (UnixTime.From(expiration))) { //TODO: Error RPC timeout taskCompletion.SetException(new Exception()); } else { var m = RpcMessages.FromByte(ea.Body); _connection.recieveChannel.BasicAck(ea.DeliveryTag, false); taskCompletion.SetResult(m); } }; return(await taskCompletion.Task); }
public void Send(string exchangeName, RpcMessage message) { try { _connection.sendChannel.ExchangeDeclare(exchangeName, "topic", false, false, null); var jsonData = RpcMessages.ToByte(message); var properties = _connection.sendChannel.CreateBasicProperties(); properties.ContentType = "application/json"; _connection.sendChannel.BasicPublish(exchangeName, "*", false, properties, jsonData); } catch (Exception ex) { _logging.LogError($"ERROR - {ex.InnerException}"); } }