/// <summary> /// 处理模块初始化过程中的任何异常,使用<seealso cref="ILoggerFacade"/>记录,并且抛出 /// <seealso cref="ModuleInitializeException"/>。 /// 可以重写这个方法提供不同的方式。 /// </summary> /// <param name="moduleInfo"> /// 产生异常的模块信息 /// </param> /// <param name="assemblyName"> /// 程序集名称。 /// </param> /// <param name="exception"> /// 引起当前错误的异常。 /// </param> /// <exception cref="ModuleInitializeException"></exception> public virtual void HandleModuleInitializationError(ModuleInfo moduleInfo, string assemblyName, Exception exception) { if (moduleInfo == null) throw new ArgumentNullException("moduleInfo"); if (exception == null) throw new ArgumentNullException("exception"); Exception moduleException; if (exception is ModuleInitializeException) { moduleException = exception; } else { if (!string.IsNullOrEmpty(assemblyName)) { moduleException = new ModuleInitializeException(moduleInfo.ModuleName, assemblyName, exception.Message, exception); } else { moduleException = new ModuleInitializeException(moduleInfo.ModuleName, exception.Message, exception); } } this.loggerFacade.Log(moduleException.ToString(), Category.Exception, Priority.High); throw moduleException; }
/// <summary> /// 初始化<see cref="LoadModuleCompletedEventArgs"/>的新实例。 /// </summary> /// <param name="moduleInfo">模块信息。</param> /// <param name="error">产生的异常。</param> public LoadModuleCompletedEventArgs(ModuleInfo moduleInfo, Exception error) { if (moduleInfo == null) { throw new ArgumentNullException("moduleInfo"); } this.ModuleInfo = moduleInfo; this.Error = error; }
/// <summary> /// 初始化一个新的<see cref="ModuleDownloadProgressChangedEventArgs"/>实例。 /// </summary> /// <param name="moduleInfo">模块信息</param> /// <param name="bytesReceived">接收到的数据大小.</param> /// <param name="totalBytesToReceive">总数据大小.</param> public ModuleDownloadProgressChangedEventArgs(ModuleInfo moduleInfo, long bytesReceived, long totalBytesToReceive) : base(CalculateProgressPercentage(bytesReceived, totalBytesToReceive), null) { if (moduleInfo == null) { throw new ArgumentNullException("moduleInfo"); } this.ModuleInfo = moduleInfo; this.BytesReceived = bytesReceived; this.TotalBytesToReceive = totalBytesToReceive; }
/// <summary> /// 添加一个静态引用的模块添加到模块信息组。 /// </summary> /// <param name="moduleInfoGroup"> /// 将要添加的信息组 /// </param> /// <param name="moduleName"> /// 模块名称 /// </param> /// <param name="moduleType"> /// 模块类型,类型需要实现<see cref="IModule"/>。 /// </param> /// <param name="dependsOn"> /// 当前模块依赖的模块名称。 /// </param> /// <returns> /// </returns> public static ModuleInfoGroup AddModule( this ModuleInfoGroup moduleInfoGroup, string moduleName, Type moduleType, params string[] dependsOn) { if (moduleType == null) throw new ArgumentNullException("moduleType"); if (moduleInfoGroup == null) throw new ArgumentNullException("moduleInfoGroup"); ModuleInfo moduleInfo = new ModuleInfo(moduleName, moduleType.AssemblyQualifiedName); moduleInfo.DependsOn.AddRange(dependsOn); moduleInfoGroup.Add(moduleInfo); return moduleInfoGroup; }
public void Initialize(ModuleInfo moduleInfo) { if (moduleInfo == null) throw new ArgumentNullException("moduleInfo"); IModule moduleInstance = null; try { moduleInstance = this.CreateModule(moduleInfo); if (moduleInstance != null) moduleInstance.Initialize(); } catch (Exception ex) { this.HandleModuleInitializationError( moduleInfo, moduleInstance != null ? moduleInstance.GetType().Assembly.FullName : null, ex); } }
public object GetInstance(Modularity.ModuleInfo moduleInfo) { Type moduleType = null; try { if (string.IsNullOrEmpty(moduleInfo.ModuleType)) { Logging.Logger.Current.Log( "10000", "Platform", "创建具体模块.", string.Format(CultureInfo.CurrentCulture, Resources.ModuleTypeIsNullOrEmpty, moduleInfo.Description), null, Logging.Category.Exception, Logging.Priority.High); ; return(null); } moduleType = Type.GetType(moduleInfo.ModuleType); object instance = GetInstance(moduleType); if (moduleInfo.InitParams != null && instance != null) { foreach (var item in moduleInfo.InitParams) { PropertyInfo property = instance.GetType().GetProperty(item.Key); property.SetValue(instance, item.Value, null); } } return(instance); } catch (Exception ex) { throw new Exception(String.Format(CultureInfo.CurrentCulture, Resources.FailedTypeInstance, moduleType.AssemblyQualifiedName), ex); } }
private void OpenTask(ModuleInfo moduleInfo) { Type moduleType = null; object instance = null; try { moduleType = Type.GetType(moduleInfo.ModuleType); instance = Activator.CreateInstance(moduleType); if (moduleInfo.InitParams != null && instance != null) { foreach (var item in moduleInfo.InitParams) { PropertyInfo property = instance.GetType().GetProperty(item.Key); property.SetValue(instance, item.Value, null); } } if (moduleInfo != null && instance != null) { WindowsManager.ClearAllWindows();//清除所有打开的窗口 //WorkHost.Visibility = Visibility.Visible; SMT.SaaS.FrameworkUI.EntityBrowser browser = new SaaS.FrameworkUI.EntityBrowser(instance); parent.Child = browser; //browser.Show<string>(DialogMode.Default, SMT.SAAS.Main.CurrentContext.Common.ParentLayoutRoot, "", (result) => { }, true); if (browser.ParentWindow != null) { WindowsManager.MaxWindow(browser.ParentWindow); } } } catch (Exception ex) { LogErrMsg("新建任务打开异常,请查看系统日志:"+ex.ToString()); } }
/// <summary> /// 根据规定的类型名称,使用容器解析一个<see cref="IModule"/>实例。 /// </summary> /// <param name="moduleInfo"> /// 要创建的模块。 /// </param> /// <returns> /// <paramref name="moduleInfo"/>指定的模块实例。 /// </returns> protected virtual IModule CreateModule(ModuleInfo moduleInfo) { if (moduleInfo == null) { throw new ArgumentNullException("moduleInfo"); } if (string.IsNullOrEmpty(moduleInfo.ModuleType)) return null; return this.CreateModule(moduleInfo.ModuleType); }
private void RaiseLoadModuleCompleted(ModuleInfo moduleInfo, Exception error) { this.RaiseLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, error)); }
private IModuleTypeLoader GetTypeLoaderForModule(ModuleInfo moduleInfo) { foreach (IModuleTypeLoader typeLoader in this.ModuleTypeLoaders) { if (typeLoader.CanLoadModuleType(moduleInfo)) { return typeLoader; } } throw new ModuleTypeLoaderNotFoundException(moduleInfo.ModuleName, String.Format(CultureInfo.CurrentCulture, Resources.NoRetrieverCanRetrieveModule, moduleInfo.ModuleName), null); }
private bool AreDependenciesLoaded(ModuleInfo moduleInfo) { IEnumerable<ModuleInfo> requiredModules = this.moduleCatalog.GetDependentModules(moduleInfo); if (requiredModules == null) { return true; } int notReadyRequiredModuleCount = requiredModules.Count(requiredModule => requiredModule.State != ModuleState.Initialized); return notReadyRequiredModuleCount == 0; }
/// <summary> /// Return the list of <see cref="ModuleInfo"/>s that <paramref name="moduleInfo"/> depends on. /// </summary> /// <remarks> /// If the <see cref="ModuleCatalog"/> was not yet validated, this method will call <see cref="Validate"/>. /// </remarks> /// <param name="moduleInfo">The <see cref="ModuleInfo"/> to get the </param> /// <returns>An enumeration of <see cref="ModuleInfo"/> that <paramref name="moduleInfo"/> depends on.</returns> public virtual IEnumerable <ModuleInfo> GetDependentModules(ModuleInfo moduleInfo) { this.EnsureCatalogValidated(); return(this.GetDependentModulesInner(moduleInfo)); }
/// <summary> /// 为加载完成的子系统进行初始化操作。 /// </summary> /// <param name="appInfo"> /// 需要进行初始化的模块信息。 /// </param> private void InitializeModule(ModuleInfo moduleInfo) { if (moduleInfo.State == ModuleState.Initializing) { this.moduleInitializer.Initialize(moduleInfo); moduleInfo.State = ModuleState.Initialized; this.RaiseLoadModuleCompleted(moduleInfo, null); } }
/// <summary> /// 如果获取到模块的程序集信息则说明程序集已经加载了,那么当前应用程序域就不需要再次加载。 /// </summary> protected virtual bool ModuleNeedsRetrieval(ModuleInfo moduleInfo) { if (moduleInfo == null) throw new ArgumentNullException("moduleInfo"); if (moduleInfo.State == ModuleState.NotStarted) { //如果获取到模块的程序集信息则说明程序集已经加载了, //那么当前应用程序域就不需要再次加载。 //若可以找到类型,则标识已经加载,并修改子系统状态为可以进行初始化。 bool isAvailable = false; if (moduleInfo.ModuleType != null) isAvailable = Type.GetType(moduleInfo.ModuleType) != null; if (isAvailable) { moduleInfo.State = ModuleState.ReadyForInitialization; } return !isAvailable; } return false; }
/// <summary> /// Returns the <see cref="ModuleInfo"/> on which the received module dependens on. /// </summary> /// <param name="moduleInfo">Module whose dependant modules are requested.</param> /// <returns>Collection of <see cref="ModuleInfo"/> dependants of <paramref name="moduleInfo"/>.</returns> protected virtual IEnumerable <ModuleInfo> GetDependentModulesInner(ModuleInfo moduleInfo) { return(this.Modules.Where(dependantModule => moduleInfo.DependsOn.Contains(dependantModule.ModuleName))); }
/// <summary> /// Adds a <see cref="ModuleInfo"/> to the <see cref="ModuleCatalog"/>. /// </summary> /// <param name="moduleInfo">The <see cref="ModuleInfo"/> to add.</param> /// <returns>The <see cref="ModuleCatalog"/> for easily adding multiple modules.</returns> public virtual void AddModule(ModuleInfo moduleInfo) { this.Items.Add(moduleInfo); }
/// <summary> /// 根据给定的模块信息,开始对系统进行加载 /// </summary> /// <param name="appInfo">子系统信息</param> private void BeginRetrievingModule(ModuleInfo moduleInfo) { ModuleInfo moduleInfoToLoadType = moduleInfo; //获取一个模块加载解析器 IModuleTypeLoader moduleTypeLoader = this.GetTypeLoaderForModule(moduleInfoToLoadType); //修改模块状态为加载中. moduleInfoToLoadType.State = ModuleState.LoadingTypes; // Delegate += 在WPF和SL中的工作方式不同. //为每一个加载器仅进行一次订阅. if (!this.subscribedToModuleTypeLoaders.Contains(moduleTypeLoader)) { moduleTypeLoader.ModuleDownloadProgressChanged += this.IModuleTypeLoader_ModuleDownloadProgressChanged; moduleTypeLoader.LoadModuleCompleted += this.IModuleTypeLoader_LoadModuleCompleted; this.subscribedToModuleTypeLoaders.Add(moduleTypeLoader); } //模块加载 moduleTypeLoader.LoadModuleType(moduleInfo); }
protected virtual void HandleModuleTypeLoadingError(ModuleInfo moduleInfo, Exception exception) { if (moduleInfo == null) throw new ArgumentNullException("moduleInfo"); ModuleTypeLoadingException moduleTypeLoadingException = exception as ModuleTypeLoadingException; if (moduleTypeLoadingException == null) { moduleTypeLoadingException = new ModuleTypeLoadingException(moduleInfo.ModuleName, exception.Message, exception); } this.loggerFacade.Log(moduleTypeLoadingException.Message, Category.Exception, Priority.High); throw moduleTypeLoadingException; }
/// <summary> /// 初始化<see cref="LoadModuleCompletedEventArgs"/>的新实例。 /// </summary> /// <param name="moduleInfo">模块信息。</param> /// <param name="error">产生的异常。</param> public LoadModuleEventArgs(object moduleInstance, ModuleInfo moduleInfo, Exception error) { if (moduleInfo == null) { throw new ArgumentNullException("moduleInfo"); } this.ModuleInstance = moduleInstance; this.ModuleInfo = moduleInfo; this.Error = error; }
private void RaiseLoadModuleCompleted(ModuleInfo moduleInfo, Exception error) { this.RaiseLoadModuleCompleted(new LoadModuleCompletedEventArgs(moduleInfo, error)); }