public T GetReceivedMessage <T>(Guid correlationId, int millisecondsTimeout) where T : class { var timeoutTask = Task.Run(() => Task.Delay(millisecondsTimeout)); while (!timeoutTask.IsCompleted) { if (_msgsReceived.ContainsKey(correlationId)) { _stopConsumer.Set(); return(_serializerFactory.DeserializeTo <T>(_msgsReceived[correlationId])); } } return(null); }
public Task <bool> VerifyMessageReceived <T>(Func <T, bool> verify, Guid correlationId, int millisecondsTimeout) where T : class { var t = Task.Run(() => { var resetEvent = new ManualResetEventSlim(false); var verified = false; using (var channel = _modelFactory.CreateModel()) { var queueName = _queueNameFactory(); var routingConfig = new RoutingConfig(queueName); var queueConfig = new QueueConfig(queueName, routingConfig, _exchange, false, true, true, null); if (_declareQueue) { channel.DeclareAndBindQueue(queueConfig); } var consumer = new EventingBasicConsumer(channel); consumer.Received += (obj, evtArgs) => { if (correlationId == new Guid(evtArgs.BasicProperties.CorrelationId)) { var msg = _serializerFactory.DeserializeTo <T>(evtArgs); verified = verify(msg); resetEvent.Set(); } }; channel.BasicConsume(queueName, true, consumer); resetEvent.Wait(millisecondsTimeout); } return(verified); }); // Allow half a second for consumer to start Thread.Sleep(500); return(t); }