public async Task <TMessage> WaitFor <TMessage>( string to, WaitOptions options = null) { WaitFilter waitFilter = FilterBuilder.To(to).Build(); return(await WaitFor <TMessage>(waitFilter, options)); }
public static async Task <EmailMessage> WaitForEmail( this MessageReceiver client, string to, WaitOptions options = null) { WaitFilter filter = FilterBuilder.To(to).Build(); return(await client.WaitFor <EmailMessage>(filter, options)); }
public async Task <TMessage> WaitFor <TMessage>( WaitFilter waitFilter = null, WaitOptions options = null) { options = options ?? new WaitOptions(); var timeoutToken = new CancellationTokenSource(TimeSpan.FromSeconds(options.Timeout)); waitFilter = waitFilter ?? new WaitFilter(); var typeName = _magnetClient.MessageMapper.ResolveTypeName <TMessage>(); waitFilter.Predicates.Add((m) => m.Type == typeName); var completion = new TaskCompletionSource <TMessage>(); timeoutToken.Token.Register(() => completion.TrySetCanceled()); try { while (!timeoutToken.Token.IsCancellationRequested) { MagnetMessage message = await _magnetClient.MessageStreamClient .GetNextAsync(_queueName, timeoutToken.Token); var match = MatchFilter(waitFilter, message); await AddReceiveReceiptAsync(message, match, timeoutToken.Token); if (match) { TMessage mapped = _magnetClient.MessageMapper.Map <TMessage>(message); completion.SetResult(mapped); break; } } } catch (Exception ex) { completion.SetException(ex); } finally { timeoutToken.Dispose(); } return(await completion.Task); }
private bool MatchFilter(WaitFilter filter, MagnetMessage message) { foreach (Predicate <MagnetMessage> predicate in filter.Predicates) { try { if (!predicate(message)) { return(false); } } catch { return(false); } } return(true); }