Пример #1
0
        /// <summary>
        /// Use the startup class specified with the options
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="applicationBuilder"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseStartup <T>(this IApplicationBuilder applicationBuilder, Action <ApplicationStartupOptions> options) where T : BaseStartup, new()
        {
            var startupOptions = new ApplicationStartupOptions();

            options?.Invoke(startupOptions);

            BaseStartup startup = Activator.CreateInstance <T>();

            applicationBuilder.Startup      = startup;
            applicationBuilder.Cancellation = startupOptions._cancellationToken;
            startup.Arguments = startupOptions.Arguments;

            Func <IApplicationBuilder, Task> onBuild = async(IApplicationBuilder appBuilder) =>
            {
                IServiceCollection serviceCollection = new ServiceCollection();

                await startup.ConfigureAsync(serviceCollection, applicationBuilder);
            };

            var configureOptions = new ConfiguredOption("Startup")
            {
                OnBuild     = onBuild,
                OnConfigure = null,
                OnStart     = null,
                OnStop      = null
            };

            applicationBuilder.ConfiguredOptions.Add(configureOptions);

            return(applicationBuilder);
        }
Пример #2
0
        /// <summary>
        /// Configures the builder to lookup all IInjectable and IInstallable classes so they are auto-registered
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseInjectables(this IApplicationBuilder applicationBuilder)
        {
            Func <IServiceCollection, Task> onConfigure = async(IServiceCollection collection) =>
            {
                var types = ReflectionLoader.Types.Where(x => typeof(IInjectable).IsAssignableFrom(x) &&
                                                         x != typeof(ISingletonInjectable) &&
                                                         x != typeof(ITransientInjectable) &&
                                                         x != typeof(IScopedInjectable)).ToList();

                var singletons = types.Where(x => x.IsInterface && typeof(ISingletonInjectable).IsAssignableFrom(x)).ToList();
                var transients = types.Where(x => x.IsInterface && typeof(ITransientInjectable).IsAssignableFrom(x)).ToList();
                var scoped     = types.Where(x => x.IsInterface && typeof(IScopedInjectable).IsAssignableFrom(x)).ToList();

                foreach (var type in singletons)
                {
                    Register <ISingletonInjectable>(collection, type, types);
                }

                foreach (var type in transients)
                {
                    Register <ITransientInjectable>(collection, type, types);
                }

                foreach (var type in scoped)
                {
                    Register <IScopedInjectable>(collection, type, types);
                }

                await Task.CompletedTask;
            };

            var config = new ConfiguredOption("Injectables")
            {
                OnBuild     = null,
                OnConfigure = onConfigure,
                OnStart     = null,
                OnStop      = null
            };


            applicationBuilder.ConfiguredOptions.Add(config);

            return(applicationBuilder);
        }
        /// <summary>
        /// Configures the builder to lookup all IInjectable and IInstallable classes so they are auto-registered
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseInstallables(this IApplicationBuilder applicationBuilder)
        {
            var actions = new List <Action <IServiceProvider> >();


            Func <IServiceCollection, Task> onConfigure = async(IServiceCollection collection) =>
            {
                var types = ReflectionLoader.Types.Where(x => typeof(IInstallable).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract).ToList();

                foreach (var type in types)
                {
                    var installable = Activator.CreateInstance(type) as IInstallable;
                    actions.Add(installable.Install(collection));
                }

                await Task.CompletedTask;
            };

            Func <IServiceProvider, Task> onStart = async(IServiceProvider container) =>
            {
                foreach (var action in actions.Where(x => x != null))
                {
                    action(container);
                }

                await Task.CompletedTask;
            };

            var config = new ConfiguredOption("Installables")
            {
                OnBuild     = null,
                OnConfigure = onConfigure,
                OnStart     = onStart,
                OnStop      = null
            };


            applicationBuilder.ConfiguredOptions.Add(config);

            return(applicationBuilder);
        }
        /// <summary>
        /// Allow the loading of IAppServices using the configured options
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseAppServices(this IApplicationBuilder applicationBuilder, Action <AppServiceManagerOptions> options)
        {
            var appServicesOptions = new AppServiceManagerOptions();

            options?.Invoke(appServicesOptions);

            Func <IServiceCollection, Task> onConfigure = async(IServiceCollection collection) =>
            {
                collection.AddSingleton <IAppServiceManager, AppServiceManager>();
                await Task.CompletedTask;
            };

            Func <IServiceProvider, Task> onStart = async(IServiceProvider container) =>
            {
                var lifestyleManager = container.GetService <IAppServiceManager>();
                await lifestyleManager.StartAllAsync(appServicesOptions);
            };

            Func <IServiceProvider, Task> onStop = async(IServiceProvider container) =>
            {
                var lifestyleManager = container.GetService <IAppServiceManager>();
                await lifestyleManager.StopAllAsync();
            };


            var config = new ConfiguredOption("AppServices")
            {
                OnBuild     = null,
                OnConfigure = onConfigure,
                OnStart     = onStart,
                OnStop      = onStop
            };


            applicationBuilder.ConfiguredOptions.Add(config);

            return(applicationBuilder);
        }