Esempio n. 1
0
        /// <summary>
        /// Intialize main application
        /// </summary>
        /// <param name="websiteRootDirectory">Website root directory</param>
        public static void Initialize(string websiteRootDirectory)
        {
            // Throw exception if already initialized
            if (Interlocked.Exchange(ref initialized, 1) != 0)
            {
                throw new InvalidOperationException("Application already initialized");
            }
            // Register core components
            Ioc.RegisterMany <CacheFactory>(ReuseType.Singleton);
            Ioc.RegisterMany <DatabaseManager>(ReuseType.Singleton);
            Ioc.RegisterMany <TJsonConverter>(ReuseType.Singleton);
            Ioc.RegisterMany <TranslateManager>(ReuseType.Singleton);
            Ioc.RegisterMany <LogManager>(ReuseType.Singleton);
#if NETCORE
            Ioc.RegisterMany <CoreAssemblyLoader>(ReuseType.Singleton);
#else
            Ioc.RegisterMany <NetAssemblyLoader>(ReuseType.Singleton);
#endif
            Ioc.RegisterMany <RoslynCompilerService>(ReuseType.Singleton);
            Ioc.RegisterMany <PluginManager>(ReuseType.Singleton);
#pragma warning disable CS0618
            Ioc.RegisterMany <ConfigManager>(ReuseType.Singleton);
            Ioc.RegisterMany <PathConfig>(ReuseType.Singleton);
            Ioc.RegisterMany <PathManager>(ReuseType.Singleton);
#pragma warning restore CS0618
            Ioc.RegisterMany <WebsiteConfigManager>(ReuseType.Singleton);
            Ioc.RegisterMany <LocalFileStorage>(ReuseType.Singleton);
            Ioc.RegisterMany <LocalPathConfig>(ReuseType.Singleton);
            Ioc.RegisterMany <LocalPathManager>(ReuseType.Singleton);
            Ioc.RegisterMany <TemplateAreaManager>(ReuseType.Singleton);
            Ioc.RegisterMany <TemplateWidgetRenderer>(ReuseType.Singleton);
            Ioc.RegisterMany <TemplateFileSystem>(ReuseType.Singleton);
            Ioc.RegisterMany <TemplateManager>(ReuseType.Singleton);
            Ioc.RegisterMany <TestManager>(ReuseType.Singleton);
            Ioc.RegisterMany <AddVersionHeaderHandler>(ReuseType.Singleton);
            Ioc.RegisterMany <DefaultErrorHandler>(ReuseType.Singleton);
            Ioc.RegisterMany <ControllerManager>(ReuseType.Singleton);
            Ioc.RegisterMany <CacheIsolateByDevice>(ReuseType.Singleton, serviceKey: "Device");
            Ioc.RegisterMany <CacheIsolateByLocale>(ReuseType.Singleton, serviceKey: "Locale");
            Ioc.RegisterMany <CacheIsolateByUrl>(ReuseType.Singleton, serviceKey: "Url");
            // Initialize core components
            LocalPathConfig.Initialize(websiteRootDirectory);
            WebsiteConfigManager.Initialize();
            PluginManager.Initialize();
            JsonNetInitializer.Initialize();
            TemplateManager.Initialize();
            ThreadPoolInitializer.Initialize();
            DatabaseManager.Initialize();
            // Initialize all plugins and controllers
            Ioc.ResolveMany <IPlugin>().ForEach(p => { });
            ControllerManager.Initialize();
            Ioc.ResolveMany <IWebsiteStartHandler>().ForEach(h => h.OnWebsiteStart());
            // Start the resident core processes
            PluginReloader.Start();
            AutomaticCacheCleaner.Start();
        }
Esempio n. 2
0
        /// <summary>
        /// Override plugin directories for testing
        /// It will remove all test files when disposed
        /// </summary>
        /// <param name="pluginDirectories">Plugin directories, default is [ "App_Data/__TestPlugins" ]</param>
        /// <param name="plugins">Plugins, default is [ "PluginA", "PluginB" ]</param>
        /// <param name="extra">Extra data, default is empty</param>
        /// <param name="pluginDirectoryIndex">Which plugin directory will use to create plugins</param>
        public TestDirectoryLayout(
            IList <string> pluginDirectories   = null,
            IList <string> plugins             = null,
            IDictionary <string, object> extra = null,
            int pluginDirectoryIndex           = 0)
        {
            OverrideIoc   = Application.OverrideIoc();
            WebsiteConfig = new WebsiteConfig()
            {
                PluginDirectories = pluginDirectories ?? new List <string>()
                {
                    "App_Data/__TestPlugins"
                },
                Plugins = plugins ?? new List <string>()
                {
                    "PluginA", "PluginB"
                },
                Extra = extra ?? new Dictionary <string, object>()
            };
            CleanupPaths = new List <string>();
            // Mock WebsiteConfigManager, LocalPathManager, PluginManager, IFileStorage
            var configManagerMock = new WebsiteConfigManager();

            configManagerMock.WebsiteConfig = WebsiteConfig;
            Application.Ioc.Unregister <WebsiteConfigManager>();
            Application.Ioc.Unregister <LocalPathManager>();
            Application.Ioc.Unregister <PluginManager>();
            Application.Ioc.Unregister <LocalFileStorage>();
            Application.Ioc.Unregister <IFileStorage>();
            Application.Ioc.RegisterInstance(configManagerMock);
            Application.Ioc.RegisterMany <LocalPathManager>(ReuseType.Singleton);
            Application.Ioc.RegisterMany <PluginManager>(ReuseType.Singleton);
            Application.Ioc.RegisterMany <LocalFileStorage>(ReuseType.Singleton);
            // Create plugin directories and plugins
            var pathManager     = Application.Ioc.Resolve <LocalPathManager>();
            var pluginManager   = Application.Ioc.Resolve <PluginManager>();
            var pluginDirectory = pathManager.GetPluginDirectories()[pluginDirectoryIndex];

            foreach (var plugin in WebsiteConfig.Plugins)
            {
                var directory = Path.Combine(pluginDirectory, plugin);
                Directory.CreateDirectory(directory);
                pluginManager.Plugins.Add(PluginInfo.FromDirectory(directory));
            }
            foreach (var directory in pathManager.GetPluginDirectories())
            {
                CleanupPaths.Add(directory);
            }
            // Clear cache to prevent file content cache
            // On linux execution speed is so fast that cause file modify time are same between tests
            Application.Ioc.ResolveMany <ICacheCleaner>().ForEach(c => c.ClearCache());
        }
Esempio n. 3
0
        public void OnRequest()
        {
            var handler = new DefaultErrorHandler();

            using (Application.OverrideIoc()) {
                var logManagerMock = new TestLogManager();
                var config         = new WebsiteConfig()
                {
                    Extra = new Dictionary <string, object>()
                };
                var configManagerMock = new WebsiteConfigManager();
                configManagerMock.WebsiteConfig = config;
                Application.Ioc.Unregister <LogManager>();
                Application.Ioc.RegisterInstance <LogManager>(logManagerMock);
                Application.Ioc.Unregister <WebsiteConfigManager>();
                Application.Ioc.RegisterInstance <WebsiteConfigManager>(configManagerMock);
                // Only return message if error occurs with ajax request
                using (HttpManager.OverrideContext("", "GET")) {
                    var request  = (HttpRequestMock)HttpManager.CurrentContext.Request;
                    var response = (HttpResponseMock)HttpManager.CurrentContext.Response;
                    request.headers["X-Requested-With"] = "XMLHttpRequest";
                    handler.OnError(new ArgumentException("some message"));
                    Assert.Equals(response.GetContentsFromBody(), "some message");
                }
                // Display status and message if the error is HttpException
                using (HttpManager.OverrideContext("", "GET")) {
                    var request  = (HttpRequestMock)HttpManager.CurrentContext.Request;
                    var response = (HttpResponseMock)HttpManager.CurrentContext.Response;
                    handler.OnError(new HttpException(404, "wrong address"));
                    var contents = response.GetContentsFromBody();
                    Assert.IsTrueWith(
                        contents.Contains("404 wrong address") && !contents.Contains("<pre>"), contents);
                }
                // Display full exception dependent on website configuration
                config.Extra[ExtraConfigKeys.DisplayFullExceptionForRequest] = true;
                using (HttpManager.OverrideContext("", "GET")) {
                    var request  = (HttpRequestMock)HttpManager.CurrentContext.Request;
                    var response = (HttpResponseMock)HttpManager.CurrentContext.Response;
                    handler.OnError(new ArgumentException("500 some error"));
                    var contents = response.GetContentsFromBody();
                    Assert.IsTrueWith(
                        contents.Contains("500 some error") && contents.Contains("<pre>"), contents);
                }
                config.Extra[ExtraConfigKeys.DisplayFullExceptionForRequest] = false;
                using (HttpManager.OverrideContext("", "GET")) {
                    var request  = (HttpRequestMock)HttpManager.CurrentContext.Request;
                    var response = (HttpResponseMock)HttpManager.CurrentContext.Response;
                    handler.OnError(new ArgumentException("500 some error"));
                    var contents = response.GetContentsFromBody();
                    Assert.IsTrueWith(
                        contents.Contains("500 some error") && !contents.Contains("<pre>"), contents);
                }
                // Test error logging
                logManagerMock.receivedLogError = false;
                using (HttpManager.OverrideContext("", "GET")) {
                    var request  = (HttpRequestMock)HttpManager.CurrentContext.Request;
                    var response = (HttpResponseMock)HttpManager.CurrentContext.Response;
                    handler.OnError(new HttpException(401, "user error"));
                    Assert.IsTrue(!logManagerMock.receivedLogError);
                }
                logManagerMock.receivedLogError = false;
                using (HttpManager.OverrideContext("", "GET")) {
                    var request  = (HttpRequestMock)HttpManager.CurrentContext.Request;
                    var response = (HttpResponseMock)HttpManager.CurrentContext.Response;
                    handler.OnError(new HttpException(500, "server error"));
                    Assert.IsTrue(logManagerMock.receivedLogError);
                }
            }
        }