コード例 #1
0
        /// <summary>
        /// Method that will be used by NServiceBus to process a message.
        /// The real work is implemented in the Handle(T message) abstract method.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Handle(T message, IMessageHandlerContext context)
        {
            try
            {
                // Create the unit of work list of messages.
                MessagesToPublish.Create();

                // business work to implement.
                await Handle(message);

                // Publish events.
                foreach (Object _event in MessagesToPublish.Events)
                {
                    try
                    {
                        await context.Publish(_event);
                    }
                    catch (System.Exception ex)
                    {
                        Logger.Technical.From(typeof(HandleMessageBase <T>)).Exception(ex).Log();
                    }
                }

                // Send commands.
                foreach (Object command in MessagesToPublish.Commands)
                {
                    try
                    {
                        await context.Send(command);
                    }
                    catch (System.Exception ex)
                    {
                        Logger.Technical.From(typeof(HandleMessageBase <T>)).Exception(ex).Log();
                    }
                }
            }
            catch (System.Exception ex)
            {
                Logger.Technical.From(typeof(HandleMessageBase <T>)).Exception(ex).Log();
            }
            finally
            {
                // Clear messages to avoid any sending of undesirable messages by
                // another message received on the bus.
                MessagesToPublish.Clear();
            }
        }
コード例 #2
0
        public MessagesScope(IContainerResolve container, ILogger logger, string iocResolveName)
        {
            Logger = logger;
            // Create the unit of work list of messages.
            MessagesToPublish.Create();

            // Search for the instance used to send commands and publish events on start of the
            // scope. So if we have a resolving issue, we know it immediately (and not after the work is done.
            if (!container.TryResolve <IEndpointConfiguration>(iocResolveName, out var endpointConfig))
            {
                logger.Technical().From <MessagesScope>().Warning($"Unable to resolve the IEndpointConfiguration with the name '{iocResolveName}'").Log();
                return;
            }

            if (null == endpointConfig.Instance)
            {
                logger.Technical().From <MessagesScope>().Warning($"Instance is null for the IEndpointConfiguration with the name '{iocResolveName}'").Log();
                return;
            }

            _instance = endpointConfig.Instance;
        }
コード例 #3
0
        public void Complete()
        {
            if (null == _instance)
            {
                Logger.Technical().From <MessagesScope>().Warning($"Unable to send any events or commands to the IEndpointConfiguration with the name.").Log();
                return;
            }

            // Publish events.
            foreach (Object _event in MessagesToPublish.Events)
            {
                try
                {
                    Logger.Technical().From <MessagesScope>().System($"Publish event: {_event.GetType().FullName}.").Log();
                    _instance.Publish(_event).Wait();
                }
                catch (Exception ex)
                {
                    Logger.Technical().From <MessagesScope>().Exception(ex).Log();
                }
            }

            // Send commands.
            foreach (Object command in MessagesToPublish.Commands)
            {
                try
                {
                    Logger.Technical().From <MessagesScope>().System($"Send command: {command.GetType().FullName}.").Log();
                    _instance.Send(command).Wait();
                }
                catch (Exception ex)
                {
                    Logger.Technical().From <MessagesScope>().Exception(ex).Log();
                }
            }

            MessagesToPublish.Clear();
        }
コード例 #4
0
ファイル: MessageScope.cs プロジェクト: GFlisch/Arc4u
 public MessageScope(ILogger <MessageScope> logger, IMessageSession messageSession, MessagesToPublish messages)
 {
     _logger         = logger;
     _messageSession = messageSession;
     _messages       = messages;
 }