/// <summary>
        ///     Registers the given <see cref="EventsContext"/> as a service in the <see cref="IServiceCollection" />.
        /// </summary>
        /// <typeparam name="T">The type of context to be registered.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
        /// <param name="optionsBuilder">
        ///     <para>
        ///         An action to configure the <see cref="EventsContextOptions" /> for the context. This provides an
        ///         alternative to performing configuration of the context by overriding the
        ///         <see cref="EventsContext.OnConfiguring" /> method in your derived context.
        ///     </para>
        ///     <para>
        ///         If an action is supplied here, the <see cref="EventsContext.OnConfiguring" /> method will still be run if it has
        ///         been overridden on the derived context. <see cref="EventsContext.OnConfiguring" /> configuration will be applied
        ///         in addition to configuration performed here.
        ///     </para>
        /// </param>
        /// <returns>The original <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddEventsContext <T>(
            this IServiceCollection services,
            Action <EventsContextOptions> optionsBuilder
            )
            where T : EventsContext
        {
            if (optionsBuilder == null)
            {
                throw new ArgumentNullException(nameof(optionsBuilder));
            }

            var options = new EventsContextOptions();

            optionsBuilder(options);

            services.TryAddScoped <EventsScope>();

            var eventsContextFactory = ActivatorUtilities.CreateFactory(
                typeof(T),
                new[] { typeof(EventsContextOptions) }
                );
            var eventsContextFactoryArgs = new object[] { options };

            services.AddSingleton(x => (T)eventsContextFactory(x, eventsContextFactoryArgs));

            services.AddScoped <IScopedAppServiceProvider, AppServiceProvider>();
            services.AddSingleton <IRootAppServiceProvider, AppServiceProvider>();

            services.AddTransient(x => x.GetRequiredService <T>().GetEventReceiversHostedService());

            return(services);
        }
Esempio n. 2
0
 /// <summary>
 ///     Creates a new <see cref="EventsContext"/>
 /// </summary>
 /// <param name="options">The options for this context.</param>
 /// <param name="rootAppServiceProvider">The application root service provider.</param>
 protected EventsContext(
     EventsContextOptions options,
     IRootAppServiceProvider rootAppServiceProvider
     )
 {
     _internalEventsContext = new Lazy <InternalEventsContext>(() => new InternalEventsContext(
                                                                   options,
                                                                   OnConfiguring,
                                                                   OnBuildingPipelines,
                                                                   OnBuildingSubscriptions,
                                                                   rootAppServiceProvider,
                                                                   this
                                                                   ), LazyThreadSafetyMode.ExecutionAndPublication);
 }
Esempio n. 3
0
 /// <summary>
 ///     Override this method to override the options supplied in the constructor.
 ///     The resulting configuration may be cached and re-used during the entire lifespan of the context.
 /// </summary>
 /// <remarks>The default implementation of this method does nothing.</remarks>
 /// <param name="options">The options of the <see cref="EventsContext"/>.</param>
 protected virtual void OnConfiguring(EventsContextOptions options)
 {
 }