예제 #1
0
        public void ValidateComposition()
        {
            // this is almost what CoreRuntime does, without
            // - managing MainDom
            // - configuring for unhandled exceptions, assembly resolution, application root path
            // - testing for database, and for upgrades (runtime level)
            // - assigning the factory to Current.Factory

            // create the very basic and essential things we need
            var logger          = new ConsoleLogger();
            var profiler        = Mock.Of <IProfiler>();
            var profilingLogger = new ProfilingLogger(logger, profiler);
            var appCaches       = AppCaches.Disabled;
            var databaseFactory = Mock.Of <IUmbracoDatabaseFactory>();
            var typeLoader      = new TypeLoader(appCaches.RuntimeCache, IOHelper.MapPath("~/App_Data/TEMP"), profilingLogger);
            var runtimeState    = Mock.Of <IRuntimeState>();

            Mock.Get(runtimeState).Setup(x => x.Level).Returns(RuntimeLevel.Run);
            var mainDom = Mock.Of <IMainDom>();

            Mock.Get(mainDom).Setup(x => x.IsMainDom).Returns(true);

            // create the register and the composition
            var register    = RegisterFactory.Create();
            var composition = new Composition(register, typeLoader, profilingLogger, runtimeState);

            composition.RegisterEssentials(logger, profiler, profilingLogger, mainDom, appCaches, databaseFactory, typeLoader, runtimeState);

            // create the core runtime and have it compose itself
            var coreRuntime = new CoreRuntime();

            coreRuntime.Compose(composition);

            // get the components
            // all of them?
            var composerTypes = typeLoader.GetTypes <IComposer>();

            // filtered
            composerTypes = composerTypes
                            .Where(x => !x.FullName.StartsWith("Umbraco.Tests"));
            // single?
            //var componentTypes = new[] { typeof(CoreRuntimeComponent) };
            var composers = new Composers(composition, composerTypes, Enumerable.Empty <Attribute>(), profilingLogger);

            // get components to compose themselves
            composers.Compose();

            // create the factory
            var factory = composition.CreateFactory();

            // at that point Umbraco is fully composed
            // but nothing is initialized (no maindom, nothing - beware!)
            // to actually *run* Umbraco standalone, better use a StandaloneRuntime
            // that would inherit from CoreRuntime and ensure everything starts

            // get components to initialize themselves
            //components.Initialize(factory);

            // and then, validate
            var lightInjectContainer = (LightInject.ServiceContainer)factory.Concrete;
            var results = lightInjectContainer.Validate().ToList();

            foreach (var resultGroup in results.GroupBy(x => x.Severity).OrderBy(x => x.Key))
            {
                Console.WriteLine($"{resultGroup.Key}: {resultGroup.Count()}");
            }

            foreach (var resultGroup in results.GroupBy(x => x.Severity).OrderBy(x => x.Key))
            {
                foreach (var result in resultGroup)
                {
                    Console.WriteLine();
                    Console.Write(ToText(result));
                }
            }

            Assert.AreEqual(0, results.Count);
        }
예제 #2
0
        /// <summary>
        /// Boots the runtime within a timer.
        /// </summary>
        protected virtual IFactory Boot(IRegister register, DisposableTimer timer)
        {
            Composition composition = null;

            try
            {
                // Setup event listener
                UnattendedInstalled += CoreRuntime_UnattendedInstalled;

                // throws if not full-trust
                new AspNetHostingPermission(AspNetHostingPermissionLevel.Unrestricted).Demand();

                // run handlers
                RuntimeOptions.DoRuntimeBoot(ProfilingLogger);

                // application caches
                var appCaches = GetAppCaches();

                // database factory
                var databaseFactory = GetDatabaseFactory();

                // configs
                var configs = GetConfigs();

                // type loader
                var typeLoader = new TypeLoader(appCaches.RuntimeCache, configs.Global().LocalTempPath, ProfilingLogger);

                // runtime state
                // beware! must use '() => _factory.GetInstance<T>()' and NOT '_factory.GetInstance<T>'
                // as the second one captures the current value (null) and therefore fails
                _state = new RuntimeState(Logger,
                                          configs.Settings(), configs.Global(),
                                          new Lazy <IMainDom>(() => _factory.GetInstance <IMainDom>()),
                                          new Lazy <IServerRegistrar>(() => _factory.GetInstance <IServerRegistrar>()))
                {
                    Level = RuntimeLevel.Boot
                };

                // TODO: remove this in netcore, this is purely backwards compat hacks with the empty ctor
                if (MainDom == null)
                {
                    MainDom = new MainDom(Logger, new MainDomSemaphoreLock(Logger));
                }


                // create the composition
                composition = new Composition(register, typeLoader, ProfilingLogger, _state, configs);
                composition.RegisterEssentials(Logger, Profiler, ProfilingLogger, MainDom, appCaches, databaseFactory, typeLoader, _state);

                // run handlers
                RuntimeOptions.DoRuntimeEssentials(composition, appCaches, typeLoader, databaseFactory);



                // register runtime-level services
                // there should be none, really - this is here "just in case"
                Compose(composition);

                // acquire the main domain - if this fails then anything that should be registered with MainDom will not operate
                AcquireMainDom(MainDom);

                // determine our runtime level
                DetermineRuntimeLevel(databaseFactory, ProfilingLogger);

                // get composers, and compose
                var composerTypes = ResolveComposerTypes(typeLoader);

                IEnumerable <Attribute> enableDisableAttributes;
                using (ProfilingLogger.DebugDuration <CoreRuntime>("Scanning enable/disable composer attributes"))
                {
                    enableDisableAttributes = typeLoader.GetAssemblyAttributes(typeof(EnableComposerAttribute), typeof(DisableComposerAttribute));
                }

                var composers = new Composers(composition, composerTypes, enableDisableAttributes, ProfilingLogger);
                composers.Compose();

                // create the factory
                _factory = Current.Factory = composition.CreateFactory();

                // determines if unattended install is enabled and performs it if required
                DoUnattendedInstall(databaseFactory);

                // determine our runtime level (AFTER UNATTENDED INSTALL)
                // TODO: Feels kinda weird to call this again
                DetermineRuntimeLevel(databaseFactory, ProfilingLogger);

                // if level is Run and reason is UpgradeMigrations, that means we need to perform an unattended upgrade
                if (_state.Reason == RuntimeLevelReason.UpgradeMigrations && _state.Level == RuntimeLevel.Run)
                {
                    // do the upgrade
                    DoUnattendedUpgrade(_factory.GetInstance <DatabaseBuilder>());

                    // upgrade is done, set reason to Run
                    _state.Reason = RuntimeLevelReason.Run;
                }

                // create & initialize the components
                _components = _factory.GetInstance <ComponentCollection>();
                _components.Initialize();
            }
            catch (Exception e)
            {
                var bfe = e as BootFailedException ?? new BootFailedException("Boot failed.", e);

                if (_state != null)
                {
                    _state.Level = RuntimeLevel.BootFailed;
                    _state.BootFailedException = bfe;
                }

                timer?.Fail(exception: bfe); // be sure to log the exception - even if we repeat ourselves

                // if something goes wrong above, we may end up with no factory
                // meaning nothing can get the runtime state, etc - so let's try
                // to make sure we have a factory
                if (_factory == null)
                {
                    try
                    {
                        _factory = Current.Factory = composition?.CreateFactory();
                    }
                    catch
                    {
                        // In this case we are basically dead, we do not have a factory but we need
                        // to report on the state so we need to manually set that, this is the only time
                        // we ever do this.
                        Current.RuntimeState = _state;
                    }
                }

                Debugger.Break();

                // throwing here can cause w3wp to hard-crash and we want to avoid it.
                // instead, we're logging the exception and setting level to BootFailed.
                // various parts of Umbraco such as UmbracoModule and UmbracoDefaultOwinStartup
                // understand this and will nullify themselves, while UmbracoModule will
                // throw a BootFailedException for every requests.
            }

            return(_factory);
        }
예제 #3
0
        public void Initialize()
        {
            Composed.Clear();
            Initialized.Clear();
            Terminated.Clear();

            var register = MockRegister();
            var factory  = MockFactory(m =>
            {
                m.Setup(x => x.TryGetInstance(It.Is <Type>(t => t == typeof(ISomeResource)))).Returns(() => new SomeResource());
                m.Setup(x => x.GetInstance(It.IsAny <Type>())).Returns <Type>((type) =>
                {
                    if (type == typeof(Composer1))
                    {
                        return(new Composer1());
                    }
                    if (type == typeof(Composer5))
                    {
                        return(new Composer5());
                    }
                    if (type == typeof(Composer5a))
                    {
                        return(new Composer5a());
                    }
                    if (type == typeof(Component5))
                    {
                        return(new Component5(new SomeResource()));
                    }
                    if (type == typeof(Component5a))
                    {
                        return(new Component5a());
                    }
                    if (type == typeof(IProfilingLogger))
                    {
                        return(new ProfilingLogger(Mock.Of <ILogger>(), Mock.Of <IProfiler>()));
                    }
                    throw new NotSupportedException(type.FullName);
                });
            });
            var composition = new Composition(register, MockTypeLoader(), Mock.Of <IProfilingLogger>(), MockRuntimeState(RuntimeLevel.Unknown));

            var types     = new[] { typeof(Composer1), typeof(Composer5), typeof(Composer5a) };
            var composers = new Composers(composition, types, Mock.Of <IProfilingLogger>());

            Assert.IsEmpty(Composed);
            composers.Compose();
            AssertTypeArray(TypeArray <Composer1, Composer5, Composer5a>(), Composed);

            var builder = composition.WithCollectionBuilder <ComponentCollectionBuilder>();

            builder.RegisterWith(register);
            var components = builder.CreateCollection(factory);

            Assert.IsEmpty(Initialized);
            components.Initialize();
            AssertTypeArray(TypeArray <Component5, Component5a>(), Initialized);

            Assert.IsEmpty(Terminated);
            components.Terminate();
            AssertTypeArray(TypeArray <Component5a, Component5>(), Terminated);
        }
예제 #4
0
        /// <summary>
        /// Boots the runtime within a timer.
        /// </summary>
        protected virtual IFactory Boot(IRegister register, DisposableTimer timer)
        {
            Composition composition = null;

            try
            {
                // throws if not full-trust
                new AspNetHostingPermission(AspNetHostingPermissionLevel.Unrestricted).Demand();

                // application caches
                var appCaches    = GetAppCaches();
                var runtimeCache = appCaches.RuntimeCache;

                // database factory
                var databaseFactory = GetDatabaseFactory();

                // configs
                var configs = GetConfigs();

                // type loader
                var localTempStorage = configs.Global().LocalTempStorageLocation;
                var typeLoader       = new TypeLoader(runtimeCache, localTempStorage, ProfilingLogger);

                // runtime state
                // beware! must use '() => _factory.GetInstance<T>()' and NOT '_factory.GetInstance<T>'
                // as the second one captures the current value (null) and therefore fails
                _state = new RuntimeState(Logger,
                                          configs.Settings(), configs.Global(),
                                          new Lazy <IMainDom>(() => _factory.GetInstance <IMainDom>()),
                                          new Lazy <IServerRegistrar>(() => _factory.GetInstance <IServerRegistrar>()))
                {
                    Level = RuntimeLevel.Boot
                };

                // main dom
                var mainDom = new MainDom(Logger);

                // create the composition
                composition = new Composition(register, typeLoader, ProfilingLogger, _state, configs);
                composition.RegisterEssentials(Logger, Profiler, ProfilingLogger, mainDom, appCaches, databaseFactory, typeLoader, _state);

                // register runtime-level services
                // there should be none, really - this is here "just in case"
                Compose(composition);

                // acquire the main domain, determine our runtime level
                AcquireMainDom(mainDom);
                DetermineRuntimeLevel(databaseFactory, ProfilingLogger);

                // get composers, and compose
                var composerTypes = ResolveComposerTypes(typeLoader);
                composition.WithCollectionBuilder <ComponentCollectionBuilder>();
                var composers = new Composers(composition, composerTypes, ProfilingLogger);
                composers.Compose();

                // create the factory
                _factory = Current.Factory = composition.CreateFactory();

                // create & initialize the components
                _components = _factory.GetInstance <ComponentCollection>();
                _components.Initialize();
            }
            catch (Exception e)
            {
                var bfe = e as BootFailedException ?? new BootFailedException("Boot failed.", e);

                if (_state != null)
                {
                    _state.Level = RuntimeLevel.BootFailed;
                    _state.BootFailedException = bfe;
                }

                timer.Fail(exception: bfe); // be sure to log the exception - even if we repeat ourselves

                // if something goes wrong above, we may end up with no factory
                // meaning nothing can get the runtime state, etc - so let's try
                // to make sure we have a factory
                if (_factory == null)
                {
                    try
                    {
                        _factory = Current.Factory = composition?.CreateFactory();
                    }
                    catch { /* yea */ }
                }

                Debugger.Break();

                // throwing here can cause w3wp to hard-crash and we want to avoid it.
                // instead, we're logging the exception and setting level to BootFailed.
                // various parts of Umbraco such as UmbracoModule and UmbracoDefaultOwinStartup
                // understand this and will nullify themselves, while UmbracoModule will
                // throw a BootFailedException for every requests.
            }

            return(_factory);
        }
예제 #5
0
        public void StandaloneTest()
        {
            IFactory factory = null;

            // clear
            foreach (var file in Directory.GetFiles(Path.Combine(IOHelper.MapPath("~/App_Data")), "NuCache.*"))
            {
                File.Delete(file);
            }

            // settings
            // reset the current version to 0.0.0, clear connection strings
            ConfigurationManager.AppSettings["umbracoConfigurationStatus"] = "";
            // fixme we need a better management of settings here (and, true config files?)

            // create the very basic and essential things we need
            var logger          = new ConsoleLogger();
            var profiler        = new LogProfiler(logger);
            var profilingLogger = new ProfilingLogger(logger, profiler);
            var appCaches       = new CacheHelper(); // fixme has HttpRuntime stuff?
            var databaseFactory = new UmbracoDatabaseFactory(logger, new Lazy <IMapperCollection>(() => factory.GetInstance <IMapperCollection>()));
            var typeLoader      = new TypeLoader(appCaches.RuntimeCache, LocalTempStorage.Default, profilingLogger);
            var mainDom         = new SimpleMainDom();
            var runtimeState    = new RuntimeState(logger, null, null, new Lazy <IMainDom>(() => mainDom), new Lazy <IServerRegistrar>(() => factory.GetInstance <IServerRegistrar>()));

            // create the register and the composition
            var register    = RegisterFactory.Create();
            var composition = new Composition(register, typeLoader, profilingLogger, runtimeState);

            composition.RegisterEssentials(logger, profiler, profilingLogger, mainDom, appCaches, databaseFactory, typeLoader, runtimeState);

            // create the core runtime and have it compose itself
            var coreRuntime = new CoreRuntime();

            coreRuntime.Compose(composition);

            // determine actual runtime level
            runtimeState.DetermineRuntimeLevel(databaseFactory, logger);
            Console.WriteLine(runtimeState.Level);
            // going to be Install BUT we want to force components to be there (nucache etc)
            runtimeState.Level = RuntimeLevel.Run;

            var composerTypes = typeLoader.GetTypes <IComposer>()                     // all of them
                                .Where(x => !x.FullName.StartsWith("Umbraco.Tests.")) // exclude test components
                                .Where(x => x != typeof(WebRuntimeComposer));         // exclude web runtime
            var composers = new Composers(composition, composerTypes, profilingLogger);

            composers.Compose();

            // must registers stuff that WebRuntimeComponent would register otherwise
            // fixme UmbracoContext creates a snapshot that it does not register with the accessor
            //  and so, we have to use the UmbracoContextPublishedSnapshotAccessor
            //  the UmbracoContext does not know about the accessor
            //  else that would be a catch-22 where they both know about each other?
            //composition.Register<IPublishedSnapshotAccessor, TestPublishedSnapshotAccessor>(Lifetime.Singleton);
            composition.Register <IPublishedSnapshotAccessor, UmbracoContextPublishedSnapshotAccessor>(Lifetime.Singleton);
            composition.Register <IUmbracoContextAccessor, TestUmbracoContextAccessor>(Lifetime.Singleton);
            composition.Register <IVariationContextAccessor, TestVariationContextAccessor>(Lifetime.Singleton);
            composition.Register <IDefaultCultureAccessor, TestDefaultCultureAccessor>(Lifetime.Singleton);
            composition.Register <ISiteDomainHelper>(_ => Mock.Of <ISiteDomainHelper>(), Lifetime.Singleton);
            composition.RegisterUnique(f => new DistributedCache());
            composition.WithCollectionBuilder <UrlProviderCollectionBuilder>().Append <DefaultUrlProvider>();
            composition.RegisterUnique <IDistributedCacheBinder, DistributedCacheBinder>();
            composition.RegisterUnique <IExamineManager>(f => ExamineManager.Instance);

            // initialize some components only/individually
            composition.WithCollectionBuilder <ComponentCollectionBuilder>()
            .Clear()
            .Append <DistributedCacheBinderComponent>();

            // configure
            composition.Configs.Add(SettingsForTests.GetDefaultGlobalSettings);
            composition.Configs.Add(SettingsForTests.GetDefaultUmbracoSettings);

            // create and register the factory
            Current.Factory = factory = composition.CreateFactory();

            // instantiate and initialize components
            var components = factory.GetInstance <ComponentCollection>();

            // do stuff
            Console.WriteLine(runtimeState.Level);

            // install
            if (true || runtimeState.Level == RuntimeLevel.Install)
            {
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var file = Path.Combine(path, "Umbraco.sdf");
                if (File.Exists(file))
                {
                    File.Delete(file);
                }

                // create the database file
                // databaseBuilder.ConfigureEmbeddedDatabaseConnection() can do it too,
                // but then it wants to write the connection string to web.config = bad
                using (var engine = new SqlCeEngine("Data Source=|DataDirectory|\\Umbraco.sdf;Flush Interval=1;"))
                {
                    engine.CreateDatabase();
                }

                //var databaseBuilder = factory.GetInstance<DatabaseBuilder>();
                //databaseFactory.Configure(DatabaseBuilder.EmbeddedDatabaseConnectionString, Constants.DbProviderNames.SqlCe);
                //databaseBuilder.CreateDatabaseSchemaAndData();

                databaseFactory.Configure(DatabaseBuilder.EmbeddedDatabaseConnectionString, Constants.DbProviderNames.SqlCe);

                var scopeProvider = factory.GetInstance <IScopeProvider>();
                using (var scope = scopeProvider.CreateScope())
                {
                    var creator = new DatabaseSchemaCreator(scope.Database, logger);
                    creator.InitializeDatabaseSchema();
                    scope.Complete();
                }
            }

            // done installing
            runtimeState.Level = RuntimeLevel.Run;

            // instantiate to register events
            // should be done by Initialize?
            // should we invoke Initialize?
            _ = factory.GetInstance <IPublishedSnapshotService>();

            // at that point, Umbraco can run!
            // though, we probably still need to figure out what depends on HttpContext...
            var contentService = factory.GetInstance <IContentService>();
            var content        = contentService.GetById(1234);

            Assert.IsNull(content);

            // create a document type and a document
            var contentType = new ContentType(-1)
            {
                Alias = "ctype", Name = "ctype"
            };

            factory.GetInstance <IContentTypeService>().Save(contentType);
            content = new Content("test", -1, contentType);
            contentService.Save(content);

            // assert that it is possible to get the document back
            content = contentService.GetById(content.Id);
            Assert.IsNotNull(content);
            Assert.AreEqual("test", content.Name);

            // need an UmbracoCOntext to access the cache
            // fixme - not exactly pretty, should not depend on HttpContext
            var httpContext        = Mock.Of <HttpContextBase>();
            var withUmbracoContext = UmbracoContext.EnsureContext(httpContext);
            var umbracoContext     = Umbraco.Web.Composing.Current.UmbracoContext;

            // assert that there is no published document
            var pcontent = umbracoContext.ContentCache.GetById(content.Id);

            Assert.IsNull(pcontent);

            // but a draft document
            pcontent = umbracoContext.ContentCache.GetById(true, content.Id);
            Assert.IsNotNull(pcontent);
            Assert.AreEqual("test", pcontent.Name);
            Assert.IsTrue(pcontent.IsDraft());

            // no published url
            Assert.AreEqual("#", pcontent.GetUrl());

            // now publish the document + make some unpublished changes
            contentService.SaveAndPublish(content);
            content.Name = "testx";
            contentService.Save(content);

            // assert that snapshot has been updated and there is now a published document
            pcontent = umbracoContext.ContentCache.GetById(content.Id);
            Assert.IsNotNull(pcontent);
            Assert.AreEqual("test", pcontent.Name);
            Assert.IsFalse(pcontent.IsDraft());

            // but the url is the published one - no draft url
            Assert.AreEqual("/test/", pcontent.GetUrl());

            // and also an updated draft document
            pcontent = umbracoContext.ContentCache.GetById(true, content.Id);
            Assert.IsNotNull(pcontent);
            Assert.AreEqual("testx", pcontent.Name);
            Assert.IsTrue(pcontent.IsDraft());

            // and the published document has a url
            Assert.AreEqual("/test/", pcontent.GetUrl());

            withUmbracoContext.Dispose();
            mainDom.Stop();
            components.Terminate();

            // exit!
        }