/// <summary> /// Stop the application context /// </summary> public void Stop() { if (this.Stopping != null) { this.Stopping(this, null); } this.m_running = false; foreach (var svc in this.m_serviceInstances.OfType <IDaemonService>().ToArray()) { Trace.TraceInformation("Stopping daemon service {0}...", svc.GetType().Name); svc.Stop(); } // Dispose services foreach (var svc in this.m_serviceInstances.OfType <IDisposable>().Where(o => o != this)) { svc.Dispose(); } AuditUtil.AuditApplicationStartStop(EventTypeCodes.ApplicationStop); if (this.Stopped != null) { this.Stopped(this, null); } this.Dispose(); }
/// <summary> /// Force stop /// </summary> public virtual void Stop() { this.Stopping?.Invoke(this, EventArgs.Empty); AuditUtil.AuditApplicationStartStop(EventTypeCodes.ApplicationStop); this.m_serviceManager.Stop(); this.m_serviceManager.Dispose(); this.m_serviceManager = null; this.m_configManager = null; this.Stopped?.Invoke(this, EventArgs.Empty); this.m_running = false; s_context = null; // tear down singleton }
/// <summary> /// Stop the application context /// </summary> public void Stop() { if (this.Stopping != null) { this.Stopping(this, null); } if (this.IsRunning) { AuditUtil.AuditApplicationStartStop(EventTypeCodes.ApplicationStop); } this.IsRunning = false; this.m_serviceProvider.Stop(); if (this.Stopped != null) { this.Stopped(this, null); } this.Dispose(); }
/// <summary> /// Start the application context /// </summary> public bool Start() { if (!this.IsRunning) { Stopwatch startWatch = new Stopwatch(); using (AuthenticationContext.EnterSystemContext()) { try { startWatch.Start(); if (this.Starting != null) { this.Starting(this, null); } // If there is no configuration manager then add the local Trace.TraceInformation("STAGE0 START: Load Configuration"); // Assign diagnostics var config = this.GetService <IConfigurationManager>().GetSection <DiagnosticsConfigurationSection>(); if (config != null) { foreach (var writer in config.TraceWriter) { Tracer.AddWriter(Activator.CreateInstance(writer.TraceWriter, writer.Filter, writer.InitializationData, config.Sources.ToDictionary(o => o.SourceName, o => o.Filter)) as TraceWriter, writer.Filter); } } #if DEBUG else { Tracer.AddWriter(new SanteDB.Core.Diagnostics.Tracing.SystemDiagnosticsTraceWriter(), System.Diagnostics.Tracing.EventLevel.LogAlways); } #endif Trace.TraceInformation("STAGE1 START: Start Dependency Injection Manager"); this.m_serviceProvider.AddServiceProvider(this); this.m_serviceProvider.Start(); Trace.TraceInformation("STAGE2 START: Notify start"); this.Started?.Invoke(this, EventArgs.Empty); this.StartTime = DateTime.Now; Trace.TraceInformation("SanteDB startup completed successfully in {0} ms...", startWatch.ElapsedMilliseconds); } catch (Exception e) { m_tracer.TraceError("Error starting up context: {0}", e); this.IsRunning = false; Trace.TraceWarning("Server is running in Maintenance Mode due to error {0}...", e.Message); } finally { AuditUtil.AuditApplicationStartStop(EventTypeCodes.ApplicationStart); startWatch.Stop(); } } this.IsRunning = true; } return(true); }
/// <summary> /// Start the daemon services /// </summary> public virtual void Start() { // Already running if (this.m_running) { return; } this.m_tracer.TraceInfo("STAGE1: Base startup initiated..."); this.m_serviceManager.AddServiceProvider(this); AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += (o, r) => { var asmLoc = Path.Combine(Path.GetDirectoryName(typeof(ApplicationContext).Assembly.Location), r.Name.Substring(0, r.Name.IndexOf(",")) + ".dll"); var retVal = Assembly.ReflectionOnlyLoad(r.Name); if (retVal != null) { return(retVal); } else if (File.Exists(asmLoc)) { return(Assembly.ReflectionOnlyLoadFrom(asmLoc)); } else { return(null); } }; // Force load the data providers from the directory try { var asmLoc = Assembly.GetEntryAssembly().Location; if (!String.IsNullOrEmpty(asmLoc)) { foreach (var f in Directory.GetFiles(Path.GetDirectoryName(asmLoc), "*.dll")) { try { var asmL = Assembly.ReflectionOnlyLoadFrom(f); if (asmL.GetExportedTypes().Any(o => o.GetInterfaces().Any(i => i.FullName == typeof(IDataConfigurationProvider).FullName))) { this.m_tracer.TraceInfo("Loading {0}...", f); Assembly.LoadFile(f); } } catch (Exception) { } } } } catch (Exception e) { this.m_tracer.TraceWarning("Could not scan startup assembly location: {0}", e); } // Authenticate as system principal for startup this.m_tracer.TraceInfo("Loading application secret"); // Set the application secret to the configured value this.Application.ApplicationSecret = this.Configuration.GetSection <SecurityConfigurationSection>().ApplicationSecret ?? this.Application.ApplicationSecret; //ModelSettings.SourceProvider = new EntitySource.DummyEntitySource(); this.Starting?.Invoke(this, EventArgs.Empty); this.m_serviceManager.Start(); this.m_tracer.TraceInfo("STAGE 3: Broadcasting startup event to services..."); this.m_running = true; this.Started?.Invoke(this, EventArgs.Empty); this.StartTime = DateTime.Now; AuditUtil.AuditApplicationStartStop(EventTypeCodes.ApplicationStart); }
/// <summary> /// Start the application context /// </summary> public bool Start() { if (!this.m_running) { Stopwatch startWatch = new Stopwatch(); try { startWatch.Start(); if (this.Starting != null) { this.Starting(this, null); } // If there is no configuration manager then add the local Trace.TraceInformation("STAGE0 START: Load Configuration"); if (this.GetService <IConfigurationManager>() == null) { throw new InvalidOperationException("Cannot find configuration manager!"); } this.m_configuration = this.GetService <IConfigurationManager>().GetSection <ApplicationServiceContextConfigurationSection>(); if (this.m_configuration == null) { throw new InvalidOperationException("Cannot load configuration, perhaps the services aren't installed?"); } // Assign diagnostics var config = this.GetService <IConfigurationManager>().GetSection <DiagnosticsConfigurationSection>(); if (config != null) { foreach (var writer in config.TraceWriter) { Tracer.AddWriter(Activator.CreateInstance(writer.TraceWriter, writer.Filter, writer.InitializationData) as TraceWriter, writer.Filter); } } #if DEBUG else { Tracer.AddWriter(new SystemDiagnosticsTraceWriter(), System.Diagnostics.Tracing.EventLevel.LogAlways); } #endif // Add this this.m_serviceInstances.Add(this); Trace.TraceInformation("STAGE1 START: Loading services"); foreach (var svc in this.m_configuration.ServiceProviders) { if (svc.Type == null) { Trace.TraceWarning("Cannot find service {0}, skipping", svc.TypeXml); } else { Trace.TraceInformation("Creating {0}...", svc.Type); var instance = Activator.CreateInstance(svc.Type); this.m_serviceInstances.Add(instance); } } Trace.TraceInformation("STAGE2 START: Starting Daemons"); foreach (var dc in this.m_serviceInstances.OfType <IDaemonService>().ToArray()) { if (!dc.Start()) { throw new Exception($"Service {dc} reported unsuccessful start"); } } Trace.TraceInformation("STAGE3 START: Notify ApplicationContext has started"); if (this.Started != null) { this.Started(this, null); } this.StartTime = DateTime.Now; AuditUtil.AuditApplicationStartStop(EventTypeCodes.ApplicationStart); } finally { startWatch.Stop(); } Trace.TraceInformation("SanteDB startup completed successfully in {0} ms...", startWatch.ElapsedMilliseconds); this.m_running = true; } return(true); }