Esempio n. 1
0
        public void RegisterDependencies(IServiceRegister serviceRegister)
        {
            IEnumerable <Type> MessageHandlers = TypeFactory.GetAllImplementations(typeof(IMessageHandler));

            MessageBus.SubscribeAll(MessageHandlers);
            foreach (Type messageHandler in MessageHandlers)
            {
                //We want to register each message handler with the service provider in case
                //there are dependencies that need to be resolved in order to use it
                if (!DependencyEngine.IsRegistered(messageHandler))
                {
                    DependencyEngine.Register(messageHandler, messageHandler, typeof(TransientServiceProvider));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a dummy container builder and registers the required services
        /// </summary>
        /// <param name="services">The MVC service collection</param>
        /// <returns>A dummy container builder</returns>
        public ContainerBuilder CreateBuilder(IServiceCollection services)
        {
            if (services is null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            DependencyEngine.Register <IServiceScopeFactory, PerRequestScopeFactory>();
            DependencyEngine.Register <IServiceProvider, DependencyEngine>(typeof(ScopedServiceProvider));
            DependencyEngine.Register <HttpContext, DefaultHttpContext>(typeof(ScopedServiceProvider));
            DependencyEngine.Register((IServiceProvider serviceProvider) => serviceProvider.GetService <HttpContext>().TryGetSession(), typeof(ScopedServiceProvider));
            DependencyEngine.Register <IControllerFactory, ControllerFactory>(typeof(SingletonServiceProvider));

            foreach (ServiceDescriptor descriptor in services)
            {
                Type?lifeTime = null;

                switch (descriptor.Lifetime)
                {
                case ServiceLifetime.Scoped:
                    lifeTime = typeof(ScopedServiceProvider);
                    break;

                case ServiceLifetime.Transient:
                    lifeTime = typeof(TransientServiceProvider);
                    break;

                case ServiceLifetime.Singleton:
                    lifeTime = typeof(SingletonServiceProvider);
                    break;
                }

                if (descriptor.ImplementationFactory != null)
                {
                    DependencyEngine.Register(descriptor.ServiceType, descriptor.ImplementationFactory, lifeTime);
                }
                else if (descriptor.ImplementationInstance != null)
                {
                    DependencyEngine.RegisterInstance(descriptor.ServiceType, descriptor.ImplementationInstance);
                }
                else
                {
                    DependencyEngine.Register(descriptor.ServiceType, descriptor.ImplementationType, lifeTime);
                }
            }

            return(new ContainerBuilder());
        }
Esempio n. 3
0
        public void Configure(IApplicationBuilder app)
        {
            try
            {
                foreach (JsonConfigurationProvider jscp in ((IConfigurationRoot)this.Configuration).Providers.OfType <JsonConfigurationProvider>())
                {
                    StaticLogger.Log($"Configuration Loaded: {jscp.Source.Path}", StaticLogger.LoggingLevel.Call);
                }

                app.UseSession();
                //app.UseHttpsRedirection();

                app.UseStaticFiles();

                //app.UseStaticFiles(new StaticFileOptions()
                //{
                //    ServeUnknownFileTypes = true
                //});

                app.UseCookiePolicy();

                app.UseWhen(context => PersistenceConfigured, appBuilder =>
                {
                    appBuilder.UseMiddleware <ServiceScope>();

                    foreach (Type t in TypeFactory.GetAllImplementations(typeof(IPenguinMiddleware)))
                    {
                        MethodInfo m = typeof(UseMiddlewareExtensions).GetMethods()
                                       .First(mi =>
                                              mi.Name == nameof(UseMiddlewareExtensions.UseMiddleware) &&
                                              !mi.ContainsGenericParameters
                                              );

                        m.Invoke(null, new object[] { appBuilder, t, Array.Empty <object>() });
                    }

                    appBuilder.UseMiddleware <ExceptionHandling>();
                });

                app.UseWhen(context => !PersistenceConfigured, appBuilder =>
                {
                    appBuilder.UseMiddleware <ConfigurePersistenceMiddleware>();
                });

                IProvideConfigurations provideConnectionStrings = new ConfigurationProviderList(
                    new JsonProvider(this.Configuration)
                    );

                DependencyEngine.Register((serviceProvider) =>
                {
                    return(provideConnectionStrings);
                });

                foreach (Type t in TypeFactory.GetAllImplementations(typeof(IRouteConfig)))
                {
                    if (Activator.CreateInstance(t) is IRouteConfig r)
                    {
                        app.UseMvc(r.RegisterRoutes);
                    }
                    else
                    {
                        throw new Exception("Somehow we managed to initialize a null route configuration");
                    }
                }

                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "areaRoute",
                        template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}"
                        );
                });

#if DEBUG
                RuntimeValidator.ExecuteTests();
#endif

                MessageBus.SubscribeAll();

                try
                {
                    ConfigureDatabase();
                }
                catch (Exception cex)
                {
                    Exceptions.Add(new StartupException(cex));
                    PersistenceConfigured = false;
                }

                StaticLogger.Log($"Application initialization completed.", StaticLogger.LoggingLevel.Final);
            }
            catch (Exception ex)
            {
                Exceptions.Add(new StartupException(ex));
                throw;
            }
        }