예제 #1
0
        /// <summary>
        /// Dispatch unprocessed messages.
        /// </summary>
        /// <param name="processManager">Dispatches all process manager commands.</param>
        private void DispatchMessages(IProcessManager processManager)
        {
            if (processManager.Commands != null)
            {
                var undispatched = new UndispatchedMessage(processManager.Id);

                context.Set <UndispatchedMessage>().Add(undispatched);// We create a undispatched object previous processing case of system crash.

                try
                {
                    while (processManager.Commands.Count > 0)
                    {// We process each command.
                        var command = processManager.Commands.Dequeue();
                        messagingRandomRetry.ExecuteAction(() => this.commandBus.Publish(command));
                    }
                }
                catch (Exception ex)
                {// Any kind of exception we recreate commands and update in database.
                    Exception outEx1 = null;
                    CQRSExceptionProcessor.HandleException(ex, out outEx1);

                    undispatched.Commands = this.serializer.Serialize(processManager.Commands);
                    sqlIncrementalRetry.ExecuteAction(() =>
                    {
                        context.SaveChanges();
                    });

                    throw outEx1;
                }
            }
        }
예제 #2
0
        public void CQRSTestExceptions()
        {
            Exception outEx = null;

            CQRSExceptionProcessor.HandleException(new Exception("ThrowException"), out outEx);
            Assert.IsTrue(outEx is SemanticException, "Invalid exception.");

            try
            {
                CQRSExceptionProcessor.Process(() =>
                {
                    throw new Exception("ThrowException");
                });
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is SemanticException, "Invalid exception.");
            }

            bool error = false;

            CQRSExceptionProcessor.Process(() =>
            {
                try
                {
                    throw new Exception("ThrowException");
                }
                catch (Exception)
                {
                    error = true;
                }
            });

            Assert.IsTrue(error, "Error must be manage inside code.");


            try
            {
                CQRSExceptionProcessor.Process(() =>
                {
                    throw new Exception("ThrowException");
                });
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is SemanticException, "Invalid exception.");
            }

            int         i  = 5;
            FakeCommand fc = null;


            error = false;

            i = CQRSExceptionProcessor.Process <int>(() =>
            {
                try
                {
                    throw new Exception("ThrowException");
                }
                catch (Exception)
                {
                    error = true;
                }
                return(i);
            });

            Assert.IsTrue(error, "Error must be manage inside code.");

            try
            {
                i = CQRSExceptionProcessor.Process <int>(() =>
                {
                    throw new Exception("ThrowException");
                });
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is SemanticException, "Invalid exception.");
                Assert.AreEqual(i, 5, "int must be 0.");
            }

            error = false;

            fc = CQRSExceptionProcessor.Process <FakeCommand>(() =>
            {
                try
                {
                    throw new Exception("ThrowException");
                }
                catch (Exception)
                {
                    error = true;
                }
                return(fc);
            });

            Assert.IsTrue(error, "Error must be manage inside code.");

            try
            {
                fc = CQRSExceptionProcessor.Process <FakeCommand>(() =>
                {
                    throw new Exception("ThrowException");
                });
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is SemanticException, "Invalid exception.");
                Assert.IsNull(fc, "object must be null.");
            }
        }