Ejemplo n.º 1
0
        protected virtual async Task <bool> ProcessMessage(string messageName, string payload)
        {
            var messageType = subsManager.GetRegisteredMessageType(messageName);

            if (messageType != null)
            {
                using (var scope = lifetimeScope.BeginLifetimeScope(AutofacScopeName))
                {
                    Type handlerType = null;
                    if (subsManager.BelongsToCommands(messageName))
                    {
                        handlerType = typeof(ICommandHandler <>);
                    }

                    if (subsManager.BelongsToEvents(messageName))
                    {
                        handlerType = typeof(IEventHandler <>);
                    }

                    if (handlerType != null)
                    {
                        var specificType = handlerType.MakeGenericType(messageType);

                        var handler = scope.Resolve(specificType);
                        var message = JsonConvert.DeserializeObject(payload, messageType);

                        try
                        {
                            await(Task) specificType.GetMethod("HandleAsync").Invoke(handler, new object[] { message });
                        }
                        catch (Exception ex)
                        {
                            // Log and silently drop the message
                            logger.LogError(ex, $"{BrokerName}: {Assembly.GetEntryAssembly().GetName().Name} HandleAsync() exception handling message {messageName}:{payload}");
                        }
                    }
                }
                return(true);
            }
            return(false);
        }