/// <summary> /// Sets up handler to handle string message, and execute it until <paramref name="maxSequence"/> is reached. /// </summary> private void ArrangeHandler(int maxSequence, Func <TestMessage, int, Task> execute) { _activator.Handle <TestMessage>(async message => { IMessageContext ctx = MessageContext.Current; ctx.Headers.TryGetValue(Headers.CorrelationId, out string cid); ctx.Headers.TryGetValue(Headers.CorrelationSequence, out string sequenceStr); int.TryParse(sequenceStr, out int sequence); // Assert context. CorrelationContext correlationContext = _correlationContextAccessor.CorrelationContext; correlationContext.Should().NotBeNull(); correlationContext.CorrelationId.Should().Be(cid); if (sequence < maxSequence) { await execute(message, sequence); } else { _tcs.SetResult(message.Value + cid); } }, _tcs.SetException); }