Example #1
0
        public async Task PublishAsync(object message)
        {
            var key           = message.GetType().Name;
            var registrations = _moduleRegistry.GetBroadcastRegistrations(key);

            var tasks = new List <Task>();

            foreach (var registration in registrations)
            {
                var action          = registration.Action;
                var receiverMessage = TranslateType(message, registration.ReceiverType);
                tasks.Add(action(receiverMessage));
            }

            await Task.WhenAll(tasks);
        }
Example #2
0
        public async Task SendAsync(IMessage message)
        {
            var module        = message.GetModuleName();
            var tasks         = new List <Task>();
            var path          = message.GetType().Name;
            var registrations = _moduleRegistry
                                .GetBroadcastRegistrations(path)
                                .Where(r => r.ReceiverType != message.GetType());

            foreach (var registration in registrations)
            {
                if (!_registrations.TryGetValue(registration.ReceiverType, out var messageAttribute))
                {
                    messageAttribute = registration.ReceiverType.GetCustomAttribute <MessageAttribute>();
                    if (message is ICommand)
                    {
                        messageAttribute = message.GetType().GetCustomAttribute <MessageAttribute>();
                        module           = registration.ReceiverType.GetModuleName();
                    }

                    _registrations.TryAdd(registration.ReceiverType, messageAttribute);
                }

                if (messageAttribute is null || !messageAttribute.Enabled)
                {
                    continue;
                }

                if (messageAttribute.Module != module)
                {
                    continue;
                }

                var action            = registration.Action;
                var receiverBroadcast = TranslateType(message, registration.ReceiverType);
                tasks.Add(action(receiverBroadcast));
            }

            await Task.WhenAll(tasks);
        }
Example #3
0
        public async Task PublishAsync(object moduleBroadcast)
        {
            var tasks         = new List <Task>();
            var path          = moduleBroadcast.GetType().Name;
            var registrations = _moduleRegistry
                                .GetBroadcastRegistrations(path)
                                .ToList();

            _logger.LogTrace($"Publishing message of type {moduleBroadcast.GetType().Name} to " +
                             $"{registrations.Count()} module receivers");

            foreach (var registration in registrations)
            {
                var action            = registration.Action;
                var receiverBroadcast = TranslateType(moduleBroadcast, registration.ReceiverType);
                tasks.Add(action(_serviceProvider, receiverBroadcast));
            }

            await Task.WhenAll(tasks);

            _logger.LogTrace($"Published message of type {moduleBroadcast.GetType().Name} to " +
                             $"{registrations.Count()} module receivers");
        }