Beispiel #1
0
        public async Task <object> InvokeDispatcherAsync(MessageDispatchInfo dispatcher, IMessage message,
                                                         CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.NotNull(dispatcher, nameof(dispatcher));
            Check.NotNull(message, nameof(message));

            if (!message.GetType().CanAssignTo(dispatcher.MessageType))
            {
                throw new ContainerException(
                          $"The message event type: {message.GetType()} being dispatched does not match or " +
                          $"derived from the dispatch information type of: {dispatcher.MessageType}.");
            }

            // Invoke the message consumers in a new lifetime scope.  This is for the case where a message
            // is received outside of the normal lifetime scope such as the one associated with the current
            // web request.
            using (var scope = AppContainer.Instance.Services.BeginLifetimeScope())
            {
                try
                {
                    // Resolve the component and call the message handler.
                    var consumer = (IMessageConsumer)scope.Resolve(dispatcher.ConsumerType);
                    return(await dispatcher.Dispatch(message, consumer, cancellationToken));
                }
                catch (Exception ex)
                {
                    Context.Logger.LogError(MessagingLogEvents.MESSAGING_EXCEPTION, "Message Dispatch Error Details.", ex);
                    throw;
                }
            }
        }
Beispiel #2
0
        public async Task <object> InvokeDispatcherInNewLifetimeScopeAsync(MessageDispatchInfo dispatcher,
                                                                           IMessage message,
                                                                           CancellationToken cancellationToken = default)
        {
            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (cancellationToken == null)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }

            if (!message.GetType().CanAssignTo(dispatcher.MessageType))
            {
                throw new ContainerException(
                          $"The message event type: {message.GetType()} being dispatched does not match or " +
                          $"derive from the dispatch information type of: {dispatcher.MessageType}.");
            }

            // Invoke the message consumers in a new lifetime scope.  This is for the case where a message
            // is received outside of the normal lifetime scope such as the one associated with the current
            // web request.

            using (var scope = CompositeApp.Instance.CreateServiceScope())
            {
                try
                {
                    // Resolve the component and call the message handler.
                    var consumer = (IMessageConsumer)scope.ServiceProvider.GetRequiredService(dispatcher.ConsumerType);
                    return(await dispatcher.Dispatch(message, consumer, cancellationToken).ConfigureAwait(false));
                }
                catch (Exception ex)
                {
                    Context.Logger.LogError(MessagingLogEvents.MessagingException, ex, "Message Dispatch Error Details.");
                    throw;
                }
            }
        }