/// <summary>
        /// Start the application context
        /// </summary>
        public static bool Start(ConsoleParameters consoleParms)
        {
            var retVal = new MiniApplicationContext(consoleParms.InstanceName);

            // Not configured
            if (!retVal.ConfigurationPersister.IsConfigured)
            {
                return(false);
            }
            else
            { // load configuration
                try
                {
                    // Set master application context
                    ApplicationServiceContext.Current = ApplicationContext.Current = retVal;
                    retVal.ConfigurationPersister.Backup(retVal.Configuration);

                    retVal.GetService <IServiceManager>().AddServiceProvider(typeof(DefaultBackupService));
                    retVal.GetService <IBackupService>().AutoRestore();

                    retVal.m_tracer = Tracer.GetTracer(typeof(MiniApplicationContext));
                    var configuration = retVal.Configuration.GetSection <DiagnosticsConfigurationSection>();
                    foreach (var tr in configuration.TraceWriter)
                    {
                        Tracer.AddWriter(Activator.CreateInstance(tr.TraceWriter, tr.Filter, tr.InitializationData, configuration.Sources.ToDictionary(o => o.SourceName, o => o.Filter)) as TraceWriter, tr.Filter);
                    }
                    var appService = retVal.GetService <IAppletManagerService>();

                    retVal.SetProgress("Loading configuration", 0.2f);

                    if (consoleParms.References != null)
                    {
                        MiniApplicationContext.LoadReferences(retVal, consoleParms.References);
                    }

                    // Does openiz.js exist as an asset?
                    var oizJs = appService.Applets.ResolveAsset("/org.santedb.core/js/santedb.js");

                    // Load all solution manifests and attempt to find their pathspec
                    if (!String.IsNullOrEmpty(consoleParms.SolutionFile))
                    {
                        LoadSolution(consoleParms.SolutionFile, appService);
                    }


                    // Load all user-downloaded applets in the data directory
                    if (consoleParms.AppletDirectories != null)
                    {
                        LoadApplets(consoleParms.AppletDirectories.OfType <String>(), appService);
                    }


                    if (oizJs?.Content != null)
                    {
                        byte[] content  = appService.Applets.RenderAssetContent(oizJs);
                        var    oizJsStr = Encoding.UTF8.GetString(content, 0, content.Length);
                        oizJs.Content = oizJsStr + (appService as MiniAppletManagerService).GetShimMethods();
                    }

                    // Set the entity source
                    EntitySource.Current = new EntitySource(retVal.GetService <IEntitySourceProvider>());

                    // Ensure data migration exists
                    var hasDatabase = retVal.ConfigurationManager.Configuration.GetSection <DcDataConfigurationSection>().ConnectionString.Count > 0;
                    try
                    {
                        // If the DB File doesn't exist we have to clear the migrations
                        if (hasDatabase && !File.Exists(retVal.ConfigurationManager.GetConnectionString(retVal.Configuration.GetSection <DcDataConfigurationSection>().MainDataSourceConnectionStringName).GetComponent("dbfile")))
                        {
                            retVal.m_tracer.TraceWarning("Can't find the SanteDB database, will re-install all migrations");
                            retVal.Configuration.GetSection <DcDataConfigurationSection>().MigrationLog.Entry.Clear();
                        }
                        retVal.SetProgress("Migrating databases", 0.6f);

                        ConfigurationMigrator migrator = new ConfigurationMigrator();
                        migrator.Ensure(hasDatabase);

                        // Prepare clinical protocols
                        //retVal.GetService<ICarePlanService>().Repository = retVal.GetService<IClinicalProtocolRepositoryService>();
                    }
                    catch (Exception e)
                    {
                        retVal.m_tracer.TraceError(e.ToString());
                        throw;
                    }
                    finally
                    {
                        retVal.ConfigurationPersister.Save(retVal.Configuration);
                    }


                    if (!retVal.Configuration.GetSection <DiagnosticsConfigurationSection>().TraceWriter.Any(o => o.TraceWriterClassXml.Contains("Console")))
                    {
                        retVal.Configuration.GetSection <DiagnosticsConfigurationSection>().TraceWriter.Add(new TraceWriterConfiguration()
                        {
                            TraceWriter = typeof(ConsoleTraceWriter),
#if DEBUG
                            Filter = EventLevel.Informational
#else
                            Filter = EventLevel.Warning
#endif
                        });
                    }



                    // Start daemons
                    retVal.GetService <IThreadPoolService>().QueueUserWorkItem((o) => retVal.Start());
                }
        /// <summary>
        /// Starts the application context using in-memory default configuration for the purposes of
        /// configuring the software
        /// </summary>
        /// <returns><c>true</c>, if temporary was started, <c>false</c> otherwise.</returns>
        public static bool StartTemporary(ConsoleParameters consoleParms)
        {
            try
            {
                // Is autoconfiguration enabled on this
                var retVal = new MiniApplicationContext(consoleParms.InstanceName);
                retVal.SetProgress("Run setup", 0);

                ApplicationServiceContext.Current = ApplicationContext.Current = retVal;


                retVal.m_tracer = Tracer.GetTracer(typeof(MiniApplicationContext));
                var configuration = retVal.Configuration.GetSection <DiagnosticsConfigurationSection>();

                foreach (var tr in configuration.TraceWriter)
                {
                    Tracer.AddWriter(Activator.CreateInstance(tr.TraceWriter, tr.Filter, tr.InitializationData, configuration.Sources.ToDictionary(o => o.SourceName, o => o.Filter)) as TraceWriter, tr.Filter);
                }

                retVal.SetProgress("Loading configuration", 0.2f);
                var appService = retVal.GetService <IAppletManagerService>();

                if (consoleParms.References != null)
                {
                    MiniApplicationContext.LoadReferences(retVal, consoleParms.References);
                }

                // Does openiz.js exist as an asset?
                var oizJs = appService.Applets.ResolveAsset("/org.santedb.core/js/santedb.js");

                // Load all solution manifests and attempt to find their pathspec
                if (!String.IsNullOrEmpty(consoleParms.SolutionFile))
                {
                    LoadSolution(consoleParms.SolutionFile, appService);
                }


                // Load all user-downloaded applets in the data directory
                if (consoleParms.AppletDirectories != null)
                {
                    LoadApplets(consoleParms.AppletDirectories.OfType <String>(), appService);
                }

                if (oizJs?.Content != null)
                {
                    byte[] content  = appService.Applets.RenderAssetContent(oizJs);
                    var    oizJsStr = Encoding.UTF8.GetString(content, 0, content.Length);
                    oizJs.Content = oizJsStr + (appService as MiniAppletManagerService).GetShimMethods();
                }

                if (!consoleParms.Restore)
                {
                    retVal.GetService <IThreadPoolService>().QueueUserWorkItem((o) => retVal.Start());
                }

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("SanteDB FATAL: {0}", e.ToString());
                return(false);
            }
        }