public void CanForwardMessageToErrorQueue()
        {
            var sharedCounter = new SharedCounter(1) { Delay = TimeSpan.FromSeconds(0.1) };

            Using(sharedCounter);

            _forwarderActivator.Handle<string>(async (bus, str) =>
            {
                await bus.Advanced.TransportMessage.Forward(ForwardedMessagesQueue, new Dictionary<string, string> {{"testheader", "OK"}});
            });

            _receiverActivator.Handle<string>(async (bus, context, str) =>
            {
                var headers = context.TransportMessage.Headers;

                if (!headers.ContainsKey("testheader"))
                {
                    sharedCounter.Fail("Could not find 'testheader' header!");
                    return;
                }

                var headerValue = headers["testheader"];
                if (headerValue != "OK")
                {
                    sharedCounter.Fail("'testheader' header had value {0}", headerValue);
                    return;
                }

                sharedCounter.Decrement();
            });

            _forwarderActivator.Bus.SendLocal("hej med dig min ven!!!").Wait();

            sharedCounter.WaitForResetEvent();
        }
        public async Task ItWorksWithCovarianceToo()
        {
            var counter = new SharedCounter(1);

            Using(counter);

            _activator.Handle<BaseMessage>(async baseMessage =>
            {
                throw new ApplicationException("1st level!!");
            });

            _activator.Handle<IFailed<BaseMessage>>(async failed =>
            {
                if (failed.Message is ConcreteMessage)
                {
                    counter.Decrement();
                    return;
                }

                counter.Fail("Did not receive the expected message!");
            });

            await _bus.SendLocal(new ConcreteMessage());

            counter.WaitForResetEvent();
        }
        public async Task ItWorks()
        {
            var counter = new SharedCounter(1);

            Using(counter);

            _activator.Handle<string>(async str =>
            {
                throw new ApplicationException("1st level!!");
            });

            _activator.Handle<IFailed<string>>(async failed =>
            {
                if (failed.Message != "hej med dig!")
                {
                    counter.Fail("Did not receive the expected message!");
                    return;
                }

                counter.Decrement();
            });

            await _bus.SendLocal("hej med dig!");

            counter.WaitForResetEvent();
        }
        public async Task FailedMessageAllowsForAccessingHeaders()
        {
            var counter = new SharedCounter(1);

            Using(counter);

            _activator.Handle<string>(async str =>
            {
                throw new ApplicationException("1st level!!");
            });

            var headersOfFailedMessage = new Dictionary<string, string>();

            _activator.Handle<IFailed<string>>(async failed =>
            {
                if (failed.Message != "hej med dig!")
                {
                    counter.Fail("Did not receive the expected message!");
                    return;
                }

                foreach (var kvp in failed.Headers)
                {
                    headersOfFailedMessage.Add(kvp.Key, kvp.Value);
                }

                Console.WriteLine();
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("ERROR DESCRIPTION:");
                Console.WriteLine();
                Console.WriteLine(failed.ErrorDescription);
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("CAUGHT EXCEPTIONS:");
                Console.WriteLine();
                Console.WriteLine(string.Join(Environment.NewLine + Environment.NewLine, failed.Exceptions));
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------------------------");
                Console.WriteLine();

                counter.Decrement();
            });

            var headers = new Dictionary<string, string>
            {
                {"custom-header", "with-a-custom-value" }
            };

            await _bus.SendLocal("hej med dig!", headers);

            counter.WaitForResetEvent();

            Console.WriteLine(string.Join(Environment.NewLine, headersOfFailedMessage.Select(kvp =>
               $"    {kvp.Key}: {kvp.Value}")));

            Assert.That(headersOfFailedMessage["custom-header"], Is.EqualTo("with-a-custom-value"));
        }