/// <summary> /// 创建新的AppDomain /// </summary> /// <param name="dirPath">AppDomain的执行文件夹</param> /// <param name="appDomainName"></param> /// <returns></returns> public static AppDomain CreateHeartServerAppDomain(string dirPath, string appDomainName) //string filePath, { AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = dirPath; setup.ConfigurationFile = "app.config"; setup.ApplicationName = appDomainName; AppDomain heartDomain = null; try { heartDomain = AppDomain.CreateDomain(appDomainName, null, setup); } catch (ArgumentNullException anex) { LogServer.WriteException("CreateDomain", anex, dirPath); if (heartDomain != null) { AppDomain.Unload(heartDomain); } heartDomain = null; } return(heartDomain); }
/// <summary> /// 创建心跳AppDomain /// </summary> /// <param name="filePath"></param> /// <param name="dirPath"></param> /// <returns></returns> private bool FilterHeartAssembly(string filePath, string dirPath) { bool b = false; string fileName = filePath.Substring(dirPath.Length + 1); if (ruleMap[RuleEnum.CheckFileName](null, fileName)) // 检验.dll文件名规则是否以 Heart_Monitor_ 开头 { fileName = fileName.Split('.')[0]; b = effectiveHeartServerMap.ContainsKey(fileName); if (!b) { AppDomain heartDomain = AppDomainHelper.CreateHeartServerAppDomain(dirPath, fileName); //filePath, if (heartDomain != null) { try { if (!ruleMap[RuleEnum.CheckAssemblyClassName](heartDomain, fileName)) { DeleteHeartDir("加载的程序集没有继承于HeartBase的HeartServer对象", dirPath); } else { HeartServerInfo info = new HeartServerInfo(); info.AssemblyFilePath = filePath; info.DirPath = dirPath; info.Name = fileName; info.Description = tempHear.GetDescription(); info.SpanInfo = TimeConfigCollection.Single[fileName]; info.OnTimeConfigChange += info_OnTimeConfigChange; effectiveHeartServerMap.TryAdd(fileName, info); } // 验证完后 不管是否与规则相符 都执行卸载AppDomain操作 AppDomain.Unload(heartDomain); heartDomain = null; b = true; } catch (FileLoadException flex) { LogServer.WriteException("Load dll", flex, filePath); AppDomain.Unload(heartDomain); heartDomain = null; DeleteHeartDir("加载的程序集没有继承于HeartBase的HeartServer对象", dirPath); } } } } return(b); }
/// <summary> /// 从内存卸载心跳服务 /// </summary> public override void Unload() { try { AppDomain.Unload(heartInfo.HeartDomain); heartInfo.HeartDomain = null; if (heartInfo.Heart != null) { heartInfo.Heart = null; } heartInfo.heartState = heartInfo.notloadedHeaert; } catch (AppDomainUnloadedException appUnloadEx) { LogServer.WriteException("Unload AppDomain", appUnloadEx); } }
void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e) { HeartServerInfo hsInfo = this[e.Exception.Source]; if (hsInfo != null) { hsInfo.ReceiveException(e.Exception); } else { LogServer.WriteException("Default AppDomain First", e.Exception); Exception subException = e.Exception.InnerException; while (subException != null) { LogServer.WriteException("InnerException First", subException); subException = subException.InnerException; } } }
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception ex = e.ExceptionObject as Exception; if (ex != null) { HeartServerInfo hsInfo = this[ex.Source]; if (hsInfo != null) { hsInfo.ReceiveException(ex); } else { LogServer.WriteException("Default AppDomain", ex); Exception subException = ex.InnerException; while (subException != null) { LogServer.WriteException("InnerException", subException); subException = subException.InnerException; } } } }