public static WebResponse HttpGet(Uri uri, string contentType) { TkDebug.AssertArgumentNull(uri, "uri", null); WebRequest request = WebRequest.Create(uri); TkTrace.LogInfo(uri.ToString()); try { WebResponse response = request.GetResponse(); if (!string.IsNullOrEmpty(contentType)) { TkDebug.Assert(response.ContentType.Contains(contentType), string.Format( ObjectUtil.SysCulture, "{0}返回的ContentType是{1},而目标是{2},两者不兼容", uri, response.ContentType, contentType), null); } return(response); } catch (WebException ex) { TkDebug.ThrowToolkitException(string.Format( ObjectUtil.SysCulture, "访问{0}时出错", uri), ex, null); return(null); } }
private void SearchXmlFile(XmlPlugInAttribute attribute, string file) { TkTrace.LogInfo($"工厂[{Description}]搜索Xml文件{file}"); ToolkitConfig configXml = ObjectUtil.CreateObject(attribute.XmlConfigType).Convert <ToolkitConfig>(); configXml.ReadXmlFromFile(file); AddXmlConfigXml(configXml, attribute.ConfigInfo); }
public void Process() { if (Status == JobStatus.Running) { return; } TkDebug.ThrowIfNoGlobalVariable(); TkTrace.LogInfo($"'{fJob}'准备启动-{DateTime.Now}"); Status = JobStatus.Running; BaseGlobalVariable.Current.BeginInvoke(EndProcess, fAction, null, null); }
internal static void AddPlugInFactory(PlugInFactoryManager manager, Assembly assembly) { Attribute[] attrs = Attribute.GetCustomAttributes(assembly, typeof(AssemblyPlugInFactoryAttribute)); if (attrs.Length > 0) { foreach (AssemblyPlugInFactoryAttribute attribute in attrs) { BasePlugInFactory factory = ObjectUtil.CreateObject( attribute.PlugInFactoryType).Convert <BasePlugInFactory>(); TkTrace.LogInfo($"添加工厂:{factory.Description}"); manager.Add(factory); } } }
private void AddPlugInXmlItem(IXmlPlugInItem item, string fileName) { Type baseClassType = GetBaseClassType(item.BaseClass); if (Contains(item.RegName)) { TkDebug.ThrowIfNoGlobalVariable(); BaseGlobalVariable.Current.AddXmlError(item.RegName, fileName, PlugInErrorType.Duplicate); } else { TkTrace.LogInfo($"工厂[{Description}]添加注册名为{item.RegName}的插件"); fXmlPlugIns.Add(item.RegName, new XmlRegItem(item.RegName, item, baseClassType, fileName)); Add(item.RegName, item, baseClassType); } }
public static WebResponse HttpPost(Uri uri, byte[] postData, string contentType) { WebRequest request = WebRequest.Create(uri); TkTrace.LogInfo(uri.ToString()); request.Method = "POST"; request.ContentType = contentType; request.ContentLength = postData.Length; using (Stream postStream = request.GetRequestStream()) { postStream.Write(postData, 0, postData.Length); } WebResponse response = request.GetResponse(); return(response); }
protected void LoadAssembly(Assembly assembly) { TkDebug.AssertArgumentNull(assembly, "assembly", this); Type currentType = null; try { TkTrace.LogInfo($"扫描{assembly.FullName}"); Type[] types = assembly.GetTypes(); Type attrType = typeof(BasePlugInAttribute); foreach (Type type in types) { if (type.IsInterface) { continue; } currentType = type; Attribute[] attrs = Attribute.GetCustomAttributes(type, attrType); if (attrs.Length == 0) { continue; } foreach (BasePlugInAttribute attribute in attrs) { FactoryManager.InternalAddCodePlugIn(this, attribute, type); } } TkTrace.LogInfo($"结束扫描{assembly.FullName}"); } catch (Exception ex) { TkTrace.LogError($"加载{assembly.FullName}出错,错误:{ex.Message}"); HandleStartedExeception("SLoadType", currentType, ex); } }
internal bool InternalAddCodePlugIn(BaseGlobalVariable globalVariable, BasePlugInAttribute attribute, Type type) { BasePlugInFactory factory = ObjectUtil.TryGetValue(fAllFactories, attribute.FactoryName); if (factory == null) { globalVariable.AddCodeError(attribute, type, PlugInErrorType.NoFactory); return(false); } bool result = factory.Add(attribute, type); if (!result) { globalVariable.AddCodeError(attribute, type, PlugInErrorType.Duplicate); } else { TkTrace.LogInfo($"在工程[{factory.Description}]中添加注册名为[{attribute.GetRegName(type)}]类型[{type}]"); } return(result); }
public void Initialize(BaseAppSetting appSetting, object application) { TkDebug.AssertArgumentNull(appSetting, "appSetting", this); fManager = new AssemblyManager(); if (NeitherContext) { AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; } // 初始化AppPath下所有的Assembly AppPathAssembly = CreateAppPathAssembly(appSetting.AppPath, fManager); TkDebug.AssertNotNull(AppPathAssembly, "CreateAppPathAssembly返回为空", this); //fLoadedAssembly = AppPathAssembly.LoadedAssembly; // AppPath下的Assembly中查找添加PlugInFactory AppPathAssembly.AddPlugInFactory(FactoryManager); // 执行AppPath下的IInitialization var inits = AppPathAssembly.CreateInitializations(); foreach (var item in inits) { try { TkTrace.LogInfo($"执行{item.GetType()}的AppStarting"); item.AppStarting(application, appSetting, this); } catch (Exception ex) { HandleStartedExeception("SAppStarting", item.GetType(), ex); } } // 搜索Code插件 foreach (var assembly in AppPathAssembly) { if (assembly == ToolkitConst.TOOLKIT_CORE_ASSEMBLY) { continue; } LoadAssembly(assembly); } fPlugIn = new PlugInAssembly(appSetting, fManager); //IEnumerable<AssemblyName> moduleAssembyNames = GetModuleAssemblies(appSetting); foreach (var assemblyName in fPlugIn) { if (!fManager.Contains(assemblyName.Name)) { var assembly = fPlugIn.LoadAssembly(fManager, NeitherContext, assemblyName); //Assembly assembly = CreateAssembly(assemblyName); //fLoadedAssemblyDict.Add(assembly.FullName, assembly); if (assembly != null) { LoadAssembly(assembly); //fLoadedAssembly.Add(assemblyName.FullName); } } } if (NeitherContext) { AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; } // 搜索Xml插件 var factories = FactoryManager.CodeFactories; foreach (var factory in factories) { factory.SearchXmlPlugIn(appSetting.XmlPath); } ProcessConfigDefaultValue(appSetting); foreach (var item in inits) { try { TkTrace.LogInfo($"执行{item.GetType()}的AppStarted"); item.AppStarted(application, appSetting, this); } catch (Exception ex) { HandleStartedExeception("SAppStarted", item.GetType(), ex); } } LogPlugError(fPlugInErrorLog); }
private void EndProcess(IAsyncResult ar) { Status = JobStatus.Idle; NextCalcTime = DateTime.Now + fInterval; TkTrace.LogInfo($"'{fJob}'停止工作-{DateTime.Now}"); }