/// <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}"); } }
/// <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(); } }); }
public void CreateNullFactoryTypeThrowsArgumentNullException() { Assert.Throws <ArgumentNullException>(() => LoggingAspect <ITestInterface> .Create( new MyTestInterface(), typeof(MyTestInterface), loggerFactory.Object, aspectConfigurationProvider, null)); }
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")); }
/// <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); } }
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")); }
/// <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); } }
/// <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); }
/// <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); } }
/// <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)); } }
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()); }
/// <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)); } }
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()); }