171516c55c281ea5d14af78cdc33f26871fb104d.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Data;
  5. using System.Text;
  6. using System.Collections;
  7. using System.Reflection;
  8. using System.Configuration;
  9. using Core.Mes.IBaseInterface;
  10. namespace Core.Mes.ServerFrameWork
  11. {
  12. public class Dispatcher : MarshalByRefObject, ICommon
  13. {
  14. #region " Construct "
  15. public Dispatcher() { }
  16. public Dispatcher(IServerPool pool)
  17. {
  18. Pool = pool;
  19. Debug = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["Debug"].Value.ToString().ToLower();
  20. }
  21. private IServerPool _pool = null;
  22. public IServerPool Pool
  23. {
  24. get { return _pool; }
  25. set { _pool = value; }
  26. }
  27. private string Debug = "false";
  28. #endregion
  29. #region " Variable "
  30. //== 子服务集合
  31. public static Hashtable _htServers = new Hashtable();
  32. //== 数据链接集合
  33. public static Hashtable htDBManager = new Hashtable();
  34. public event GetStatusInfoHandler getStatusInfo;
  35. public delegate void GetStatusInfoHandler(string info);
  36. protected virtual void SetStatusMessage(string info)
  37. {
  38. if (getStatusInfo != null)
  39. {
  40. getStatusInfo(info);
  41. }
  42. }
  43. #endregion
  44. #region 内存释放
  45. public void MemoryDispose()
  46. {
  47. int MemoryMaxSize = 100;
  48. int.TryParse(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["MemoyMaxSize"].Value.ToString(), out MemoryMaxSize);
  49. long memorysize = System.Diagnostics.Process.GetCurrentProcess().WorkingSet64;
  50. int PagedMemorySize = (int)memorysize / (1024 * 1024);
  51. if (PagedMemorySize > MemoryMaxSize)//进程占用内存>100M 做GC.
  52. {
  53. try
  54. {
  55. GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
  56. }
  57. catch
  58. {
  59. }
  60. System.Diagnostics.Process.GetCurrentProcess().Dispose();
  61. }
  62. }
  63. #endregion
  64. #region MethodTimeLog
  65. private static object lockObj = new object();
  66. public void MethodTimeLog(TimeSpan tspan, object[] objs, object[] args)
  67. {
  68. string path = string.Format(@"./log/Method/MethodTimeLog_{0}.txt", System.DateTime.Now.ToString("yyyy_MM_dd"));
  69. lock (lockObj)
  70. {
  71. using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
  72. {
  73. StringBuilder sbtxt = new StringBuilder();
  74. sbtxt.AppendLine("==============================================");
  75. sbtxt.AppendLine(string.Format("LogWriteTime:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  76. sbtxt.AppendLine(string.Format("Time:{0}", tspan.TotalMilliseconds));
  77. sbtxt.AppendLine(string.Format("ClassName:{0}", objs[0].ToString()));
  78. sbtxt.AppendLine(string.Format("MethodName:{0}", objs[1].ToString()));
  79. if (args != null)
  80. {
  81. foreach (object obj in args)
  82. {
  83. if (obj != null)
  84. {
  85. sbtxt.AppendLine(string.Format("Parameters:{0}", obj.ToString()));
  86. }
  87. }
  88. }
  89. sbtxt.AppendLine("==============================================");
  90. sw.WriteLine(sbtxt.ToString());
  91. }
  92. }
  93. }
  94. public void MethodErrLog(Exception ex, object[] objs, object[] args)
  95. {
  96. string path = string.Format(@"./log/Method/MethodErrLog_{0}.txt", System.DateTime.Now.ToString("yyyy_MM_dd"));
  97. lock (lockObj)
  98. {
  99. using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
  100. {
  101. StringBuilder sbtxt = new StringBuilder();
  102. sbtxt.AppendLine("==============================================");
  103. sbtxt.AppendLine(string.Format("LogWriteTime:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  104. sbtxt.AppendLine(string.Format("ClassName:{0}", objs[0].ToString()));
  105. sbtxt.AppendLine(string.Format("MethodName:{0}", objs[1].ToString()));
  106. if (args != null)
  107. {
  108. foreach (object obj in args)
  109. {
  110. if (obj != null)
  111. {
  112. sbtxt.AppendLine(string.Format("Parameters:{0}", obj.ToString()));
  113. }
  114. }
  115. }
  116. sbtxt.AppendLine(ex.Message);
  117. sbtxt.AppendLine(ex.StackTrace);
  118. sbtxt.AppendLine("==============================================");
  119. sw.WriteLine(sbtxt.ToString());
  120. }
  121. }
  122. }
  123. public void DebugLog(object[] objs, object[] args)
  124. {
  125. string path = string.Format(@"./log/Method/DebugLog_{0}.txt", System.DateTime.Now.ToString("yyyy_MM_dd"));
  126. lock (lockObj)
  127. {
  128. if (File.Exists(Path.GetFullPath(path)))
  129. {
  130. FileInfo fileInfo = new FileInfo(Path.GetFullPath(path));
  131. if (fileInfo.Length / (1024 * 1024) > 6)
  132. {
  133. File.Delete(path);
  134. }
  135. }
  136. using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
  137. {
  138. StringBuilder sbtxt = new StringBuilder();
  139. sbtxt.AppendLine("==============================================");
  140. sbtxt.AppendLine(string.Format("LogWriteTime:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
  141. sbtxt.AppendLine(string.Format("ClassName:{0}", objs[0].ToString()));
  142. sbtxt.AppendLine(string.Format("MethodName:{0}", objs[1].ToString()));
  143. if (args != null)
  144. {
  145. foreach (object obj in args)
  146. {
  147. if (obj != null)
  148. {
  149. sbtxt.AppendLine(string.Format("Parameters:{0}", obj.ToString()));
  150. }
  151. }
  152. }
  153. sbtxt.AppendLine("==============================================");
  154. sw.WriteLine(sbtxt.ToString());
  155. }
  156. }
  157. }
  158. #endregion
  159. #region " 调用服务 "
  160. /// <summary>
  161. /// 服务端公开给客户端的调用后台服务的方法
  162. /// </summary>
  163. /// <param name="message"></param>
  164. /// <returns></returns>
  165. public ReturnObject MethodHandler(CallingMessage message, ValidateInfo validateInfo)
  166. {
  167. //step1:
  168. MemoryDispose();
  169. //step2:
  170. string serverName = message.ServerName;
  171. string assemblyName = message.AssemblyName;
  172. string className = message.ClassName;
  173. string methodName = message.MethodName;
  174. object[] args = message.args;
  175. ReturnObject rtnObj = new ReturnObject();
  176. MethodInfo myMethod = null;
  177. try
  178. {
  179. if (Pool.HtComponent.Contains(className))
  180. {
  181. myMethod = Pool.GetType().GetMethod("HandleMethod");
  182. try
  183. {
  184. //rtnObj = Pool.HandleMethod(className, methodName, args);
  185. DateTime startTime = DateTime.Now;
  186. if (Debug == "true")
  187. {
  188. DebugLog(new object[] { className, methodName }, args);
  189. }
  190. rtnObj = (ReturnObject)myMethod.Invoke(Pool, new object[] { className, methodName, args });
  191. DateTime endTime = DateTime.Now;
  192. TimeSpan tspan = endTime - startTime;
  193. int MethodTime = 30;
  194. int.TryParse(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["MethodTime"].Value.ToString().ToLower(), out MethodTime);
  195. if (tspan.TotalMilliseconds > (MethodTime * 1000))
  196. {
  197. MethodTimeLog(tspan, new object[] { className, methodName }, args);
  198. }
  199. return rtnObj;
  200. }
  201. catch (Exception ex)
  202. {
  203. MethodErrLog(ex, new object[] { className, methodName }, args);
  204. return new ReturnObject(null, ex.Message);
  205. }
  206. finally
  207. {
  208. //if (myMethod != null) myMethod = null;
  209. try
  210. {
  211. if (rtnObj.RealObject != null)
  212. {
  213. rtnObj.RealObject = null;
  214. }
  215. if (rtnObj.RealDataSet != null)
  216. {
  217. rtnObj.RealDataSet = null;
  218. }
  219. }
  220. catch { }
  221. }
  222. }
  223. else
  224. {
  225. return new ReturnObject(null, "未找到请求的服务!");
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. return new ReturnObject(null, "服务调用发生异常! \n" + ex.Message);
  231. }
  232. }
  233. #endregion
  234. #region " Common Handler "
  235. //=======================================================
  236. // 用来确保当创建 Singleton 时, 第一个实例永远不会过期
  237. //=======================================================
  238. public override object InitializeLifetimeService()
  239. {
  240. return null;
  241. }
  242. #endregion
  243. }
  244. }