private async void OnNext(object sender, TransportMessageEventArgs <Message> eventArgs)
        {
            var key = _commandTypes.FirstOrDefault(x => x.Value == eventArgs.Message.Command).Key;

            if (key != Commands.None && _handlers.TryGetValue(key, out HashSet <SubscriptionInfo> hashset))
            {
                using var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME);
                foreach (var sub in hashset)
                {
                    try
                    {
                        //Here we resolve handler type from DI container
                        //using subscriptionInfo
                        var handler = scope.Resolve(sub.HandlerType);
                        if (handler is null)
                        {
                            continue;
                        }
                        //Dynamic magic ;)
                        var concreteType = typeof(IHandler <>).MakeGenericType(new Type[] { typeof(Message) });
                        await(Task) concreteType.GetMethod("HandleAsync").Invoke(handler, new object[] { eventArgs.Message, eventArgs.TransportName });
                    }
                    catch (Exception e)
                    {
                        //TODO handling
                        OnException(this, e);
                    }
                }
            }
        }
예제 #2
0
        public async Task ReceiveMessage(Message message, IPrincipal principal,
                                         CancellationToken cancellationToken = default(CancellationToken))
        {
            var messageReceivedHandlers = MessageReceived;

            if (messageReceivedHandlers != null)
            {
                if (_messageJournal != null)
                {
                    await _messageJournal.Append(message, MessageJournalCategory.Received, cancellationToken);
                }

                var args = new TransportMessageEventArgs(message, principal, cancellationToken);
                await messageReceivedHandlers(this, args);
            }
        }