Наследование: OnMethodBoundaryAspect
Пример #1
0
        /// <summary>
        /// Read specific message of the given read queue identified by the messageId parameter and copy it to one or more specified write queues
        /// </summary>
        /// <param name="messageId"></param>
        public void Copy(string messageId)
        {
            if (_errorQueue == null || !_errorQueue.IsInitialized || _readQueue == null || !_readQueue.IsInitialized)
            {
                throw new BusException("Bus has not been configured for copying messages from the read queue. Did you forget to call DefineReadQueue and/or DeineErrorQueue on BusBuilder?");
            }

            if (!_writeQueueManager.HasWriteQueues)
            {
                throw new BusException("Bus has not been configured for returning messages to a write queue. Did you forget to call DefineWriteQueue on BusBuilder?");
            }

            try
            {
                var message            = _readQueue.GetMessageBy(messageId);
                var context            = new WriteMessageContext(_writeQueueManager.GetWriteQueues().First());
                var copyMessageHandler = new CopyMessageHandler(context, _config, _logger);
                var loggingAspect      = new LoggingAspect(copyMessageHandler, SendOperation, _logger);
                loggingAspect.Handle(message);
            }
            catch (Exception ex)
            {
                throw new BusException($"A problem occurred copying message: {messageId} - error: {ex}");
            }
        }
Пример #2
0
        /// <summary>
        /// Reads messages off a queue as they arrive, deserializes them into the
        /// specified type T and invokes registered handlers. This operation is
        /// asynchnronous meaning registered handlers will be invoked on the
        /// background thread.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void ReceiveAsync <T>()
        {
            if (_readQueue == null || !_readQueue.IsInitialized)
            {
                throw new BusException("Bus has not been configured for receiving messages. Did you forget to call DefineReadQueue on BusBuilder?");
            }

            _readQueue.ReceiveAsync(message => {
                var context = new ReadMessageContext(_errorQueue, _readQueue);
                var receiveMessageHandler     = new ReceiveMessageHandler <T>(_handlers, _config, _logger);
                var retryAspect               = new RetryAspect(receiveMessageHandler, _config, _logger);
                var removeFromReadQueueAspect = new RemoveFromReadQueueAspect(retryAspect, context, _config, _logger);
                var moveToErrorQueueAspect    = new MoveToErrorQueueAspect(removeFromReadQueueAspect, context, _config, _logger);
                var loggingAspect             = new LoggingAspect(moveToErrorQueueAspect, ReceiveOperation, _logger);
                var transactionAspect         = new TransactionAspect(loggingAspect, _logger);
                var discardAspect             = new DiscardFailuresAspect(transactionAspect, _config, _logger);
                var failFastAspect            = new FailFastAspect(discardAspect, _config, _logger);
                failFastAspect.Handle(message);

                if (failFastAspect.Failed)
                {
                    _readQueue.StopReceiveAsync();
                }
            });
        }
Пример #3
0
 public void CreateNullFactoryTypeThrowsArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => LoggingAspect <ITestInterface> .Create(
                                               new MyTestInterface(),
                                               typeof(MyTestInterface),
                                               loggerFactory.Object,
                                               aspectConfigurationProvider,
                                               null));
 }
Пример #4
0
        public void TestIntercept()
        {
            var mockedLoggingService = new Mock <ILoggingService>();                   //为ILoggingService创建一个伪造对象
            var loggingAspect        = new LoggingAspect(mockedLoggingService.Object); //使用伪造对象的Object属性实例化LoggingAspect
            var mockedInvocation     = new Mock <IInvocation>();                       //为IInvoation对象创建一个伪造对象

            loggingAspect.Intercept(mockedInvocation.Object);
            mockedLoggingService.Verify(x => x.Write("Log start"));//使用伪造对象的Verify验证Write方法是否像期待的那样执行
            mockedLoggingService.Verify(x => x.Write("Log end"));
        }
Пример #5
0
        /// <summary>
        /// Read messages containg the given type T off the defined
        /// error queue and moves them to the user defined read queue
        /// </summary>
        public void ReturnErrorMessages()
        {
            if ((_errorQueue == null || !_errorQueue.IsInitialized) || (_readQueue == null || !_readQueue.IsInitialized))
            {
                throw new BusException("Bus has not been configured for returning messages to the read queue. Did you forget to call DefineReadQueue and/or DeineErrorQueue on BusBuilder?");
            }

            foreach (Message message in _errorQueue.GetAllMessages())
            {
                var context = new ReadMessageContext(_errorQueue, _readQueue);
                var returnToSourceHandler = new ReturnToSourceHandler(context, _logger);
                var loggingAspect         = new LoggingAspect(returnToSourceHandler, ReturnOperation, _logger);
                loggingAspect.Handle(message);
            }
        }
Пример #6
0
        public void TestIntercept()
        {
            var mockedLoggingService = new Mock <ILoggingService>();
            var args = new MethodExecutionArgs(null, Arguments.Empty);

            ObjectFactory.Initialize(x =>
                                     x.For <ILoggingService>().Use(mockedLoggingService.Object));
            var loggingAspect = new LoggingAspect();

            loggingAspect.RuntimeInitialize(null);
            loggingAspect.OnEntry(args);
            loggingAspect.OnSuccess(args);

            mockedLoggingService.Verify(x => x.Write("Log start"));
            mockedLoggingService.Verify(x => x.Write("Log end"));
        }
Пример #7
0
        /// <summary>
        /// Takes a given type T and serializes it to json before
        /// wrapping it in an MSMQ message and placing it on a queue.
        /// </summary>
        public void Send <T>(T dto)
        {
            if (!_writeQueueManager.HasWriteQueues)
            {
                throw new BusException("Bus has not been configured for sending messages. Did you forget to call DefineWriteQueue on BusBuilder?");
            }

            foreach (var writeQueue in _writeQueueManager.GetWriteQueues())
            {
                var message            = CreateMsmqMessageFromDto(dto);
                var context            = new WriteMessageContext(writeQueue);
                var sendMessageHandler = new SendMessageHandler(context, _config, _logger);
                var loggingAspect      = new LoggingAspect(sendMessageHandler, SendOperation, _logger);
                var transactionAspect  = new TransactionAspect(loggingAspect, _logger);
                transactionAspect.Handle(message);
            }
        }
Пример #8
0
        /// <summary>
        ///     The test initialize.
        /// </summary>
        public LoggingAspectTests()
        {
            loggerFactory = new Mock <ILoggerFactory>();
            logger        = new Mock <ILogger>();
            aspectConfigurationProvider = new InMemoryAspectConfigurationProvider();
            var aspectConfiguration = new AspectConfiguration(new ServiceDescriptor(AspectRegistrationTests.IInterfaceType, AspectRegistrationTests.MyTestInterfaceType, ServiceLifetime.Transient));

            aspectConfiguration.AddEntry(LoggingAspectFactory.LoggingAspectFactoryType, methodsToIntercept: AspectRegistrationTests.IInterfaceType.GetMethods());
            aspectConfiguration.AddEntry(LoggingAspectFactory.LoggingAspectFactoryType, methodsToIntercept: AspectRegistrationTests.IInterfaceType.GetMethods());
            aspectConfigurationProvider.AddEntry(aspectConfiguration);
            loggerFactory.Setup(x => x.CreateLogger(typeof(MyTestInterface).FullName)).Returns(logger.Object);
            instance = LoggingAspect <ITestInterface> .Create(
                new MyTestInterface(),
                typeof(MyTestInterface),
                loggerFactory.Object,
                aspectConfigurationProvider,
                LoggingAspectFactory.LoggingAspectFactoryType);
        }
Пример #9
0
        /// <summary>
        /// Reads messages off a queue, deserializes them into the
        /// specified type T and invokes registered handlers. Useful when
        /// you want to control when messages are processed i.e. at a set
        /// time every day, for example.
        /// </summary>
        public void Receive <T>()
        {
            if (_readQueue == null || !_readQueue.IsInitialized)
            {
                throw new BusException("Bus has not been configured for receiving messages. Did you forget to call DefineReadQueue on BusBuilder?");
            }

            foreach (Message message in _readQueue.GetAllMessages())
            {
                var context = new ReadMessageContext(_errorQueue, _readQueue);
                var receiveMessageHandler     = new ReceiveMessageHandler <T>(_handlers, _config, _logger);
                var retryAspect               = new RetryAspect(receiveMessageHandler, _config, _logger);
                var removeFromReadQueueAspect = new RemoveFromReadQueueAspect(retryAspect, context, _logger);
                var moveToErrorQueueAspect    = new MoveToErrorQueueAspect(removeFromReadQueueAspect, context, _logger);
                var loggingAspect             = new LoggingAspect(moveToErrorQueueAspect, ReceiveOperation, _logger);
                var transactionAspect         = new TransactionAspect(loggingAspect, _logger);
                transactionAspect.Handle(message);
            }
        }
Пример #10
0
        /// <summary>
        /// Read specific message off the defined error queue and move it to the user defined read queue
        /// </summary>
        public void ReturnErrorMessage(string id)
        {
            if ((_errorQueue == null || !_errorQueue.IsInitialized) || (_readQueue == null || !_readQueue.IsInitialized))
            {
                throw new BusException("Bus has not been configured for returning messages to the read queue. Did you forget to call DefineReadQueue and/or DeineErrorQueue on BusBuilder?");
            }

            try
            {
                Message message = _errorQueue.GetMessageBy(id);
                var     context = new ReadMessageContext(_errorQueue, _readQueue);
                var     returnToSourceHandler = new ReturnToSourceHandler(context, _logger);
                var     loggingAspect         = new LoggingAspect(returnToSourceHandler, ReturnOperation, _logger);
                loggingAspect.Handle(message);
            }
            catch (Exception)
            {
                throw new BusException(String.Format("Message with id {0} was not found on the error queue", id));
            }
        }
Пример #11
0
        public void Proceeds_With_Invocation()
        {
            var invokedMethodInfo = new Mock <MethodInfo>();

            invokedMethodInfo.Setup(m => m.Name).Returns("Test Method");

            var invocation = new Mock <IInvocation>();

            invocation.Setup(m => m.Method).Returns(invokedMethodInfo.Object);
            invocation.Setup(m => m.Proceed());

            var logger = new Mock <ILogger>();

            logger.Setup(m => m.Info(It.IsAny <string>()));

            var loggingAspect = new LoggingAspect(logger.Object);

            loggingAspect.Intercept(invocation.Object);

            invocation.Verify(m => m.Proceed(), Times.Once());
        }
Пример #12
0
        /// <summary>
        /// Read messages containing the given type T off the defined
        /// error queue and moves them to the user defined read queue
        /// </summary>
        public void ReturnAllErrorMessages()
        {
            if ((_errorQueue == null || !_errorQueue.IsInitialized) || (_readQueue == null || !_readQueue.IsInitialized))
            {
                throw new BusException("Bus has not been configured for returning messages to the read queue. Did you forget to call DefineReadQueue and/or DeineErrorQueue on BusBuilder?");
            }

            try
            {
                foreach (Message message in _errorQueue.GetAllMessages())
                {
                    var context = new ReadMessageContext(_errorQueue, _readQueue);
                    var returnToSourceHandler = new ReturnToSourceHandler(context, _logger);
                    var loggingAspect         = new LoggingAspect(returnToSourceHandler, ReturnOperation, _logger);
                    loggingAspect.Handle(message);
                }
            }
            catch (Exception ex)
            {
                throw new BusException(String.Format("A problem occurred retreiving messages from the error queue: {0}", ex));
            }
        }
Пример #13
0
        public void Logs_Method_Name_With_Arguments_Before_Invocation()
        {
            var expectedLogMessage = "m_inv:Test Method,m_args:1,2";

            var invokedMethodInfo = new Mock <MethodInfo>();

            invokedMethodInfo.Setup(m => m.Name).Returns("Test Method");

            var invocation = new Mock <IInvocation>();

            invocation.Setup(m => m.Method).Returns(invokedMethodInfo.Object);
            invocation.Setup(m => m.Arguments).Returns(new object[] { 1, 2 });

            var logger = new Mock <ILogger>();

            logger.Setup(m => m.Info(It.IsAny <string>()));

            var loggingAspect = new LoggingAspect(logger.Object);

            loggingAspect.Intercept(invocation.Object);

            logger.Verify(m => m.Info(expectedLogMessage), Times.Once());
        }