public void VerifyNullDataDoesNotThrow()
        {
            var exception  = new NullDataException();
            var dictionary = new Dictionary <string, string>();

            var failedQueue = new Address("TheErrorQueue", "TheErrorQueueMachine");

            ExceptionHeaderHelper.SetExceptionHeaders(dictionary, exception, failedQueue, "The reason", false);
        }
        public void ExceptionMessageIsTruncated()
        {
            var exception  = new Exception(new string('x', (int)Math.Pow(2, 15)));
            var dictionary = new Dictionary <string, string>();

            ExceptionHeaderHelper.SetExceptionHeaders(dictionary, exception);

            Assert.AreEqual((int)Math.Pow(2, 14), dictionary["NServiceBus.ExceptionInfo.Message"].Length);
        }
        public void VerifyDataIsSet()
        {
            var exception = GetAnException();

            exception.Data["TestKey"] = "MyValue";

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

            var failedQueue = new Address("TheErrorQueue", "TheErrorQueueMachine");

            ExceptionHeaderHelper.SetExceptionHeaders(dictionary, exception, failedQueue, "The reason", false);

            Assert.AreEqual("MyValue", dictionary["NServiceBus.ExceptionInfo.Data.TestKey"]);
        }
        public void VerifyHeadersAreSet()
        {
            var exception  = GetAnException();
            var dictionary = new Dictionary <string, string>();

            ExceptionHeaderHelper.SetExceptionHeaders(dictionary, exception);

            Assert.AreEqual("System.AggregateException", dictionary["NServiceBus.ExceptionInfo.ExceptionType"]);
            Assert.AreEqual(exception.ToString(), dictionary["NServiceBus.ExceptionInfo.StackTrace"]);
            Assert.IsTrue(dictionary.ContainsKey("NServiceBus.TimeOfFailure"));

            Assert.AreEqual("System.Exception", dictionary["NServiceBus.ExceptionInfo.InnerExceptionType"]);
            Assert.AreEqual("A fake help link", dictionary["NServiceBus.ExceptionInfo.HelpLink"]);
            Assert.AreEqual("NServiceBus.Core.Tests", dictionary["NServiceBus.ExceptionInfo.Source"]);
        }
        async Task TrySendDelayedMessageToErrorQueue(DelayedMessage timeout, Exception exception, CancellationToken cancellationToken)
        {
            try
            {
                bool success = await delayedMessageStore.Remove(timeout, cancellationToken).ConfigureAwait(false);

                if (!success)
                {
                    // Already dispatched
                    return;
                }

                Dictionary <string, string> headersAndProperties = MsmqUtilities.DeserializeMessageHeaders(timeout.Headers);

                ExceptionHeaderHelper.SetExceptionHeaders(headersAndProperties, exception);
                headersAndProperties[FaultsHeaderKeys.FailedQ] = timeoutsQueueTransportAddress;
                foreach (KeyValuePair <string, string> pair in faultMetadata)
                {
                    headersAndProperties[pair.Key] = pair.Value;
                }

                Log.InfoFormat("Move {0} to error queue", timeout.MessageId);
                using (var transportTx = new TransactionScope(txOption, transactionOptions, TransactionScopeAsyncFlowOption.Enabled))
                {
                    var transportTransaction = new TransportTransaction();
                    transportTransaction.Set(Transaction.Current);

                    var outgoingMessage    = new OutgoingMessage(timeout.MessageId, headersAndProperties, timeout.Body);
                    var transportOperation = new TransportOperation(outgoingMessage, new UnicastAddressTag(errorQueue));
                    await dispatcher.Dispatch(new TransportOperations(transportOperation), transportTransaction, CancellationToken.None)
                    .ConfigureAwait(false);

                    transportTx.Complete();
                }
            }
            catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested)
            {
                //Shutting down
                Log.Debug("Aborted sending delayed message to error queue due to shutdown.");
            }
            catch (Exception ex)
            {
                Log.Error($"Failed to move delayed message {timeout.MessageId} to the error queue {errorQueue} after {timeout.NumberOfRetries} failed attempts at dispatching it to the destination", ex);
            }
        }
        public void VerifyLegacyHeadersAreSet()
        {
            var exception  = GetAnException();
            var dictionary = new Dictionary <string, string>();

            var failedQueue = new Address("TheErrorQueue", "TheErrorQueueMachine");

            ExceptionHeaderHelper.SetExceptionHeaders(dictionary, exception, failedQueue, "The reason", true);
            Assert.AreEqual("The reason", dictionary["NServiceBus.ExceptionInfo.Reason"]);
            Assert.AreEqual("System.AggregateException", dictionary["NServiceBus.ExceptionInfo.ExceptionType"]);
            var stackTrace = dictionary["NServiceBus.ExceptionInfo.StackTrace"];

            Assert.IsTrue(stackTrace.StartsWith(@"   at NServiceBus.Core.Tests.Utils.ExceptionHeaderHelperTests.MethodThatThrows1() in "));
            Assert.AreEqual("TheErrorQueue@TheErrorQueueMachine", dictionary[FaultsHeaderKeys.FailedQ]);
            Assert.IsTrue(dictionary.ContainsKey("NServiceBus.TimeOfFailure"));

            Assert.AreEqual("System.Exception", dictionary["NServiceBus.ExceptionInfo.InnerExceptionType"]);
            Assert.AreEqual("A fake help link", dictionary["NServiceBus.ExceptionInfo.HelpLink"]);
            Assert.AreEqual("NServiceBus.Core.Tests", dictionary["NServiceBus.ExceptionInfo.Source"]);
        }
Пример #7
0
        async Task <ErrorHandleResult> OnError(ErrorContext errorContext, CancellationToken cancellationToken)
        {
            Log.Error($"OnError {errorContext.Message.MessageId}", errorContext.Exception);

            if (errorContext.ImmediateProcessingFailures < numberOfRetries)
            {
                return(ErrorHandleResult.RetryRequired);
            }

            var message = errorContext.Message;

            ExceptionHeaderHelper.SetExceptionHeaders(message.Headers, errorContext.Exception);
            message.Headers[FaultsHeaderKeys.FailedQ] = errorContext.ReceiveAddress;
            foreach (var pair in faultMetadata)
            {
                message.Headers[pair.Key] = pair.Value;
            }

            var outgoingMessage    = new OutgoingMessage(message.NativeMessageId, message.Headers, message.Body);
            var transportOperation = new TransportOperation(outgoingMessage, new UnicastAddressTag(errorQueue));
            await dispatcher.Dispatch(new TransportOperations(transportOperation), errorContext.TransportTransaction, cancellationToken).ConfigureAwait(false);

            return(ErrorHandleResult.Handled);
        }