Exemple #1
0
        // Helper to invoke any handler config attributes on this handler type or its base classes.
        private static void InvokeAttributesOnHandlerType(EventHandlerDescriptor descriptor, Type type)
        {
            Contract.Requires(descriptor != null);

            if (type == null)
            {
                return;
            }

            // Initialize base class before derived classes (same order as ctors).
            InvokeAttributesOnHandlerType(descriptor, type.BaseType);

            // Check for attribute
            object[] attrs = type.GetCustomAttributes(inherit: false);
            for (int i = 0; i < attrs.Length; i++)
            {
                object attr = attrs[i];
                IHandlerConfiguration handlerConfig = attr as IHandlerConfiguration;
                if (handlerConfig != null)
                {
                    ProcessorConfiguration originalConfig = descriptor.Configuration;
                    CommandHandlerSettings settings       = new CommandHandlerSettings(originalConfig);
                    //// handlerConfig.Initialize(settings, descriptor);
                    descriptor.Configuration = ProcessorConfiguration.ApplyHandlerSettings(settings, originalConfig);
                }
            }
        }
        private static Task InvokeActionAsyncCore(EventHandlerContext context, CancellationToken cancellationToken)
        {
            Contract.Requires(context != null);
            Contract.Requires(context.Descriptor != null);

            EventHandlerDescriptor handlerDescriptor = context.Descriptor;

            return(handlerDescriptor.ExecuteAsync(context, cancellationToken));
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandHandlerContext"/> class.
        /// </summary>
        /// <param name="request">The handler request.</param>
        /// <param name="descriptor">The handler descriptor.</param>
        public EventHandlerContext(EventHandlerRequest request, EventHandlerDescriptor descriptor)
            : this()
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            if (request.Configuration == null)
            {
                throw Error.Argument("request");
            }

            this.Configuration = request.Configuration;
            this.Request       = request;
            this.Event         = request.Event;
            this.Descriptor    = descriptor;
            this.User          = this.Configuration.Services.GetPrincipalProvider().Principal;
        }
Exemple #4
0
        private static Task InvokeHandlerAsync(EventHandlerDescriptor descriptor, EventHandlerRequest request, CancellationToken cancellationToken)
        {
            Contract.Requires(descriptor != null);
            Contract.Requires(request != null);

            IEventHandler handler = descriptor.CreateHandler(request);

            if (handler == null)
            {
                throw CreateHandlerNotFoundException(descriptor);
            }

            request.RegisterForDispose(handler, true);
            EventHandlerContext context = new EventHandlerContext(request, descriptor);

            context.Handler      = handler;
            handler.EventContext = context;
            EventFilterGrouping filterGrouping = descriptor.GetFilterGrouping();

            ServicesContainer   servicesContainer = request.Configuration.Services;
            IEventHandlerResult result            = new EventHandlerFilterResult(context, servicesContainer, filterGrouping.EventHandlerFilters);

            return(result.ExecuteAsync(cancellationToken));
        }
Exemple #5
0
        private ConcurrentDictionary <Type, EventHandlersDescriptor> InitializeHandlerInfoCache()
        {
            ConcurrentDictionary <Type, EventHandlersDescriptor> concurrentDictionary = new ConcurrentDictionary <Type, EventHandlersDescriptor>();
            Dictionary <Type, ILookup <Type, Type> >             cache = this.handlerTypeCache.Cache;

            foreach (KeyValuePair <Type, ILookup <Type, Type> > current in cache)
            {
                Type eventType = current.Key;
                Collection <EventHandlerDescriptor> descriptors = new Collection <EventHandlerDescriptor>();
                foreach (IGrouping <Type, Type> group in current.Value)
                {
                    foreach (Type handlerType in group)
                    {
                        EventHandlerDescriptor descriptor = new EventHandlerDescriptor(this.configuration, eventType, handlerType);
                        descriptors.Add(descriptor);
                    }
                }

                EventHandlersDescriptor eventDescriptor = new EventHandlersDescriptor(eventType.Name, descriptors);
                concurrentDictionary.TryAdd(eventType, eventDescriptor);
            }

            return(concurrentDictionary);
        }
Exemple #6
0
        private static CommandHandlerNotFoundException CreateHandlerNotFoundException(EventHandlerDescriptor descriptor)
        {
            Contract.Requires(descriptor != null);

            return(new CommandHandlerNotFoundException(descriptor.MessageType));
        }