/// <summary> /// 系统启动时调用 /// </summary> internal static async Task OnZeroStart() { //Debug.Assert(!HaseActiveObject); ZeroTrace.SystemLog("Application", "[OnZeroStart>>"); //using (OnceScope.CreateScope(ZeroObjects, ResetObjectActive)) { List <Task> tasks = new List <Task>(); foreach (var obj in ZeroObjects.Values.ToArray()) { try { ZeroTrace.SystemLog(obj.ServiceName, $"Try start by {StationState.Text(obj.RealState)}"); tasks.Add(Task.Run(obj.OnStart)); } catch (Exception e) { ZeroTrace.WriteException(obj.ServiceName, e, "*Start"); } } Task.WaitAll(tasks.ToArray()); //等待所有对象信号(全开或全关) await ActiveSemaphore.WaitAsync(); } ApplicationState = StationState.Run; ZeroTrace.SystemLog("Application", "<<OnZeroStart]"); }
/// <summary> /// 注册对象 /// </summary> public static bool RegistZeroObject(IService obj) { if (!ZeroObjects.TryAdd(obj.ServiceName, obj)) { return(false); } ZeroTrace.SystemLog(obj.ServiceName, "RegistZeroObject"); if (ApplicationState >= StationState.Initialized) { try { obj.OnInitialize(); ZeroTrace.SystemLog(obj.ServiceName, "Initialize"); } catch (Exception e) { ZeroTrace.WriteException(obj.ServiceName, e, "Initialize"); } } //if (obj.GetType().IsSubclassOf(typeof(ZeroStation))) //{ // ZeroDiscover discover = new ZeroDiscover // { // StationName = obj.StationName // }; // discover.FindApies(obj.GetType()); // ZeroDiscover.DiscoverApiDocument(obj.GetType()); //} if (ApplicationState != StationState.Run) { return(true); } try { ZeroTrace.SystemLog(obj.ServiceName, "Start"); obj.OnStart(); } catch (Exception e) { ZeroTrace.WriteException(obj.ServiceName, e, "Start"); } return(true); }
/// <summary> /// 系统启动时调用 /// </summary> internal static void OnZeroInitialize() { ZeroTrace.SystemLog("Application", "[OnZeroInitialize>>"); //using (OnceScope.CreateScope(ZeroObjects)) { foreach (var obj in ZeroObjects.Values.ToArray()) { try { obj.OnInitialize(); ZeroTrace.SystemLog(obj.ServiceName, "Initialize"); } catch (Exception e) { ZeroTrace.WriteException(obj.ServiceName, e, "*Initialize"); } } ZeroTrace.SystemLog("Application", "<<OnZeroInitialize]"); } }
/// <summary> /// 注销时调用 /// </summary> internal static void OnZeroDestory() { ZeroTrace.SystemLog("Application", "[OnZeroDestory>>"); var array = ZeroObjects.Values.ToArray(); ZeroObjects.Clear(); foreach (var obj in array) { try { ZeroTrace.SystemLog("OnZeroDestory", obj.ServiceName); obj.OnDestory(); } catch (Exception e) { ZeroTrace.WriteException("OnZeroDestory", e, obj.ServiceName); } } ZeroTrace.SystemLog("Application", "<<OnZeroDestory]"); GC.Collect(); ZeroTrace.SystemLog("Application", "[OnZeroDispose>>"); foreach (var obj in array) { try { ZeroTrace.SystemLog("OnZeroDispose", obj.ServiceName); obj.Dispose(); } catch (Exception e) { ZeroTrace.WriteException("OnZeroDispose", e, obj.ServiceName); } } ZeroTrace.SystemLog("Application", "<<OnZeroDispose]"); }