示例#1
0
        /// <summary>
        ///     Make sure we startup everything after WPF instantiated
        /// </summary>
        /// <param name="e">StartupEventArgs</param>
        protected override async void OnStartup(StartupEventArgs e)
        {
            Log.Debug().WriteLine("Starting application startup");

            _bootstrapper.Configure();

            // Register the UI SynchronizationContext, this can be retrieved by specifying the name "ui" for the argument
            _bootstrapper.Builder.RegisterInstance(SynchronizationContext.Current).Named <SynchronizationContext>("ui");
            _bootstrapper.Builder.RegisterInstance(TaskScheduler.FromCurrentSynchronizationContext()).Named <TaskScheduler>("ui");
            // The following makes sure that Caliburn.Micro is correctly initialized on the right thread and Execute.OnUIThread works
            // Very important to do this, after all assemblies are loaded!
            _caliburnMicroBootstrapper = new CaliburnMicroBootstrapper(_bootstrapper);
            _bootstrapper.Builder.RegisterInstance(_caliburnMicroBootstrapper).As <IService>().SingleInstance();

            // Prepare the bootstrapper
            await _bootstrapper.InitializeAsync().ConfigureAwait(true);

            _caliburnMicroBootstrapper.Initialize();

            // Now check if there is a lock, if so we invoke OnAlreadyRunning and return
            if (_bootstrapper.IsAlreadyRunning)
            {
                var exitCode = OnAlreadyRunning?.Invoke() ?? -1;
                Shutdown(exitCode);
                return;
            }

            // Start Dapplo, services which need a UI context need to configure this
            await _bootstrapper.StartupAsync().ConfigureAwait(true);

            Log.Debug().WriteLine("Finished application startup");
            // This also triggers the Caliburn.Micro.BootstrapperBase.OnStartup
            base.OnStartup(e);
        }
        public async Task Test_StartupException()
        {
            bool isDisposed = false;

            AssemblyUtils.SetEntryAssembly(GetType().Assembly);

            var applicationConfig = ApplicationConfigBuilder
                                    .Create()
                                    .WithApplicationName(ApplicationName)
                                    .WithScanDirectories(
                TestAssemblyDirectories
                )
                                    // Add all file starting with Dapplo and ending on .dll
                                    .WithAssemblyPatterns("Dapplo*")
                                    .BuildApplicationConfig();

            using (var bootstrapper = new ApplicationBootstrapper(applicationConfig))
            {
                bootstrapper.Configure();

                // Makes the startup break
                bootstrapper.Builder.Register(context => true);

                bootstrapper.RegisterForDisposal(SimpleDisposable.Create(() => isDisposed = true));

                // Initialize, so we can export
                Assert.True(await bootstrapper.InitializeAsync().ConfigureAwait(false), "Not initialized");

                // Start the composition, and IStartupActions
                await Assert.ThrowsAsync <NotSupportedException>(async() => await bootstrapper.StartupAsync().ConfigureAwait(false));
            }
            // Dispose automatically calls IShutdownActions
            Assert.True(isDisposed);
        }
示例#3
0
        /// <summary>
        ///     Make sure we startup everything after WPF instanciated
        /// </summary>
        /// <param name="e">StartupEventArgs</param>
        protected override async void OnStartup(StartupEventArgs e)
        {
            // Enable UI access for different Dapplo packages, especially the UiContext.RunOn
            // This only works here, not before the Application is started and not later
            UiContext.Initialize();

            _bootstrapper.Configure();

            // Register the UI SynchronizationContext, this can be retrieved by specifying the name "ui" for the argument
            _bootstrapper.Builder.RegisterInstance(SynchronizationContext.Current).Named <SynchronizationContext>("ui");
            _bootstrapper.Builder.RegisterInstance(TaskScheduler.FromCurrentSynchronizationContext()).Named <TaskScheduler>("ui");

            // Prepare the bootstrapper
            await _bootstrapper.InitializeAsync().ConfigureAwait(true);

            // The following makes sure that Caliburn.Micro is correctly initialized on the right thread and Execute.OnUIThread works
            // Very important to do this, after all assemblies are loaded!
            _caliburnMicroBootstrapper = new CaliburnMicroBootstrapper(_bootstrapper);
            _caliburnMicroBootstrapper.Initialize();

            // Now check if there is a lock, if so we invoke OnAlreadyRunning and return
            if (_bootstrapper.IsAlreadyRunning)
            {
                var exitCode = OnAlreadyRunning?.Invoke() ?? -1;
                Shutdown(exitCode);
                return;
            }

            // Start Dapplo, do not use configure-await false here, so the OnStartup doesn't have any issues
            await _bootstrapper.StartupAsync().ConfigureAwait(true);

            // This also triggers the Caliburn.Micro.BootstrapperBase.OnStartup
            base.OnStartup(e);
        }