public async Task <OperationResult <TMsg> > GetAsync(Func <ICarrot <TMsg>, Task> handle) { var operationResult = new OperationResult <TMsg>(); try { await Task.Run(async() => { var result = _thisChannel.Channel.BasicGet(_consumeFromQueue, _autoAck); if (result != null) { var msg = _deserialize(result.Body); var carrot = new Carrot <TMsg>(msg, result.DeliveryTag, _thisChannel); await handle(carrot); operationResult.IsSuccess = true; operationResult.State = OperationState.Get; operationResult.Message = msg; } else { operationResult.IsSuccess = false; operationResult.State = OperationState.GetFailed; } }); } catch (System.Exception ex) { operationResult.IsSuccess = false; operationResult.Error = ex; } return(operationResult); }
private async void HandleReceived(object channel, BasicDeliverEventArgs deliverd) { Carrot <TMsg> carrot = null; try { TMsg message = _deserialize(deliverd.Body); carrot = new Carrot <TMsg>(message, deliverd.DeliveryTag, _thisChannel) { MessageProperties = deliverd.BasicProperties }; await _receive(carrot); if (_autoAck == false) { await carrot.SendAckAsync(); } } catch (System.Exception ex) { if (carrot != null) { await carrot.SendNackAsync(withRequeue : true); } } }