Example #1
0
 public Task StartAsync(InboxConfig inboxConfig, CancellationToken cancellationToken = default)
 {
     InboxConfig = inboxConfig;
     Inbox       = (IEventInbox)ServiceProvider.GetRequiredService(inboxConfig.ImplementationType);
     Timer.Start(cancellationToken);
     return(Task.CompletedTask);
 }
Example #2
0
    public override async Task ProcessFromInboxAsync(
        IncomingEventInfo incomingEvent,
        InboxConfig inboxConfig)
    {
        var eventType = EventTypes.GetOrDefault(incomingEvent.EventName);

        if (eventType == null)
        {
            return;
        }

        var eventData  = Serializer.Deserialize(incomingEvent.EventData, eventType);
        var exceptions = new List <Exception>();

        await TriggerHandlersAsync(eventType, eventData, exceptions, inboxConfig);

        if (exceptions.Any())
        {
            ThrowOriginalExceptions(eventType, exceptions);
        }
    }
Example #3
0
 public static void UseMySQL <TDbContext>(this InboxConfig outboxConfig)
     where TDbContext : IHasEventInbox
 {
     outboxConfig.ImplementationType = typeof(ISqlRawDbContextEventInbox <TDbContext>);
 }
Example #4
0
 public abstract Task ProcessFromInboxAsync(
     IncomingEventInfo incomingEvent,
     InboxConfig inboxConfig);
Example #5
0
    protected virtual async Task TriggerHandlersAsync(Type eventType, object eventData, List <Exception> exceptions, InboxConfig inboxConfig = null)
    {
        await new SynchronizationContextRemover();

        foreach (var handlerFactories in GetHandlerFactories(eventType))
        {
            foreach (var handlerFactory in handlerFactories.EventHandlerFactories)
            {
                await TriggerHandlerAsync(handlerFactory, handlerFactories.EventType, eventData, exceptions, inboxConfig);
            }
        }

        //Implements generic argument inheritance. See IEventDataWithInheritableGenericArgument
        if (eventType.GetTypeInfo().IsGenericType&&
            eventType.GetGenericArguments().Length == 1 &&
            typeof(IEventDataWithInheritableGenericArgument).IsAssignableFrom(eventType))
        {
            var genericArg = eventType.GetGenericArguments()[0];
            var baseArg    = genericArg.GetTypeInfo().BaseType;
            if (baseArg != null)
            {
                var baseEventType   = eventType.GetGenericTypeDefinition().MakeGenericType(baseArg);
                var constructorArgs = ((IEventDataWithInheritableGenericArgument)eventData).GetConstructorArgs();
                var baseEventData   = Activator.CreateInstance(baseEventType, constructorArgs);
                await PublishToEventBusAsync(baseEventType, baseEventData);
            }
        }
    }
Example #6
0
    protected virtual async Task TriggerHandlerAsync(IEventHandlerFactory asyncHandlerFactory, Type eventType,
                                                     object eventData, List <Exception> exceptions, InboxConfig inboxConfig = null)
    {
        using (var eventHandlerWrapper = asyncHandlerFactory.GetHandler())
        {
            try
            {
                var handlerType = eventHandlerWrapper.EventHandler.GetType();

                if (inboxConfig?.HandlerSelector != null &&
                    !inboxConfig.HandlerSelector(handlerType))
                {
                    return;
                }

                using (CurrentTenant.Change(GetEventDataTenantId(eventData)))
                {
                    if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(ILocalEventHandler <>)))
                    {
                        var method = typeof(ILocalEventHandler <>)
                                     .MakeGenericType(eventType)
                                     .GetMethod(
                            nameof(ILocalEventHandler <object> .HandleEventAsync),
                            new[] { eventType }
                            );

                        await((Task)method.Invoke(eventHandlerWrapper.EventHandler, new[] { eventData }));
                    }
                    else if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(IDistributedEventHandler <>)))
                    {
                        var method = typeof(IDistributedEventHandler <>)
                                     .MakeGenericType(eventType)
                                     .GetMethod(
                            nameof(IDistributedEventHandler <object> .HandleEventAsync),
                            new[] { eventType }
                            );

                        await((Task)method.Invoke(eventHandlerWrapper.EventHandler, new[] { eventData }));
                    }
                    else
                    {
                        throw new AbpException("The object instance is not an event handler. Object type: " + handlerType.AssemblyQualifiedName);
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
        }
    }
Example #7
0
    protected virtual async Task TriggerHandlerAsync(IEventHandlerFactory asyncHandlerFactory, Type eventType,
                                                     object eventData, List <Exception> exceptions, InboxConfig inboxConfig = null)
    {
        using (var eventHandlerWrapper = asyncHandlerFactory.GetHandler())
        {
            try
            {
                var handlerType = eventHandlerWrapper.EventHandler.GetType();

                if (inboxConfig?.HandlerSelector != null &&
                    !inboxConfig.HandlerSelector(handlerType))
                {
                    return;
                }

                using (CurrentTenant.Change(GetEventDataTenantId(eventData)))
                {
                    await EventHandlerInvoker.InvokeAsync(eventHandlerWrapper.EventHandler, eventData, eventType);
                }
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
        }
    }
Example #8
0
 public Emails(IOptions <InboxConfig> inboxConfig)
 {
     this.inboxConfig = inboxConfig.Value;
 }
Example #9
0
 public static void UseNpgsql <TDbContext>(this InboxConfig outboxConfig)
     where TDbContext : IHasEventInbox
 {
     outboxConfig.ImplementationType = typeof(IPostgreSqlDbContextEventInbox <TDbContext>);
 }
Example #10
0
 public static void UseOracle <TDbContext>(this InboxConfig outboxConfig)
     where TDbContext : IHasEventInbox
 {
     outboxConfig.ImplementationType = typeof(IOracleDbContextEventInbox <TDbContext>);
 }
 public static void UseMongoDbContext <TMongoDbContext>(this InboxConfig outboxConfig)
     where TMongoDbContext : IHasEventInbox
 {
     outboxConfig.ImplementationType = typeof(IMongoDbContextEventInbox <TMongoDbContext>);
 }