969ee43fc040e31559667d5e92ebe470a719a27e.svn-base 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4. using System.Windows.Forms;
  5. using Core.Mes.IBaseInterface;
  6. using System.Drawing;
  7. using System.ComponentModel;
  8. namespace Core.Mes.ClientFrameWork
  9. {
  10. /// <summary>
  11. /// RemotingHelp 的摘要说明。
  12. /// </summary>
  13. public class RemotingHelp
  14. {
  15. public RemotingHelp()
  16. {
  17. }
  18. public DataSet ServerUrlList
  19. {
  20. set
  21. {
  22. DataSet ds = value;
  23. if (ds != null || ds.Tables.Count > 0)
  24. {
  25. _htServerUrlList = new Hashtable();
  26. foreach (DataRow dr in ds.Tables[0].Rows)
  27. {
  28. _htServerUrlList.Add(dr["ServerName"].ToString(), dr["Url"].ToString() + "/" + dr["ServerName"]);
  29. }
  30. }
  31. ds.Dispose();
  32. }
  33. }
  34. private Hashtable _htServerUrlList = new Hashtable();
  35. private Hashtable _htRemoteServer = new Hashtable();
  36. private bool GetServer(string serverName)
  37. {
  38. try
  39. {
  40. if (!_htRemoteServer.Contains(serverName))
  41. _htRemoteServer.Add(serverName, (ICommon)Activator.GetObject(typeof(ICommon), _htServerUrlList[serverName].ToString()));
  42. }
  43. catch (Exception ex)
  44. {
  45. Console.WriteLine(ex.Message);
  46. return false;
  47. }
  48. return true;
  49. }
  50. public object ExecuteMethod(CallingMessage par, out string errorInfo)
  51. {
  52. errorInfo = "";
  53. if (!FindRemoteServer(par.ServerName))
  54. {
  55. errorInfo = "未找到请求的服务!";
  56. return null;
  57. }
  58. try
  59. {
  60. ReturnObject rtnObj = (_htRemoteServer[par.ServerName] as ICommon).MethodHandler(par, new ValidateInfo());
  61. errorInfo = rtnObj.ErrMessage;
  62. return rtnObj.RealObject;
  63. }
  64. catch (Exception ex)
  65. {
  66. errorInfo = ex.Message;
  67. return null;
  68. }
  69. }
  70. public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out string errorInfo)
  71. {
  72. //== 解析参数
  73. CallingMessage par = new CallingMessage();
  74. par.ServerName = serverName;
  75. par.ClassName = className;
  76. par.MethodName = methodName;
  77. par.args = args;
  78. par.ServerType = MesServerType.IComponentContainServer;
  79. par.TransType = NetWorkTransType.Remoting;
  80. par.visitType = VisitType.Method;
  81. errorInfo = "";
  82. return ExecuteMethod(par, out errorInfo);
  83. }
  84. private bool FindRemoteServer(string serverName)
  85. {
  86. if (!_htRemoteServer.Contains(serverName))
  87. return GetServerUrl(serverName);
  88. return true;
  89. }
  90. private bool GetServerUrl(string serverName)
  91. {
  92. if (!_htServerUrlList.Contains(serverName))
  93. return false;
  94. return GetServer(serverName);
  95. }
  96. public void InitServerUrlList(string serverName, string url)
  97. {
  98. if (_htServerUrlList.Count == 0)
  99. _htServerUrlList.Add(serverName, url);
  100. }
  101. public AsyncExecute AsyncExecute
  102. {
  103. get
  104. {
  105. return new AsyncExecute(this);
  106. }
  107. }
  108. }
  109. #region add maliang 2012-4-12
  110. public class AsyncExecute
  111. {
  112. private RemotingHelp _RemotingHelp = null;
  113. public AsyncExecute(RemotingHelp pRemotingHelp)
  114. {
  115. _RemotingHelp = pRemotingHelp;
  116. }
  117. private Form m_ParentBusy;
  118. /// <summary>
  119. /// 需要显示忙的窗体。
  120. /// </summary>
  121. public Form ParentBusy
  122. {
  123. get { return m_ParentBusy; }
  124. set { m_ParentBusy = value; }
  125. }
  126. private Point m_BusyLocation = new Point(0, 0);
  127. /// <summary>
  128. /// 需要显示忙的窗体。
  129. /// </summary>
  130. public Point BusyLocation
  131. {
  132. get { return m_BusyLocation; }
  133. set { m_BusyLocation = value; }
  134. }
  135. /// <summary>
  136. /// 请求完成的委托
  137. /// </summary>
  138. /// <param name="returnObj">返回的对象</param>
  139. /// <param name="returnMsg">返回的消息内容</param>
  140. public delegate void CompleteEventHandler(object returnObj, string returnMsg);
  141. /// <summary>
  142. /// 请求完成的事件,如果返回的对象需要处理,请订阅该事件
  143. /// </summary>
  144. public event CompleteEventHandler OnComplete;
  145. /// <summary>
  146. /// 参数
  147. /// </summary>
  148. private CallingMessage CallingMessage;
  149. /// <summary>
  150. /// 错误信息
  151. /// </summary>
  152. private string errorInfo = string.Empty;
  153. /// <summary>
  154. /// 返回的对象
  155. /// </summary>
  156. private object returnObj;
  157. public void AsyncExecuteMethod(CallingMessage par)
  158. {
  159. CallingMessage = par;
  160. BackgroundWorker bw = new BackgroundWorker();
  161. bw.DoWork += new DoWorkEventHandler(DoWork);
  162. bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
  163. bw.WorkerReportsProgress = true;
  164. bw.RunWorkerAsync();
  165. }
  166. /// <summary>
  167. /// 请求完成
  168. /// </summary>
  169. void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  170. {
  171. if (ParentBusy != null)
  172. {
  173. BusyCtl.Stop();
  174. ParentBusy.Controls.Remove(BusyCtl);
  175. }
  176. string msg = string.Empty;
  177. //如果订阅了该事件并且返回的结果没有问题才调用完成的事件
  178. if (OnComplete == null)
  179. {
  180. throw new Exception("调用接口请求时未订阅OnComplete事件,请在调用AsyncExecuteMethod请求对象是订阅OnComplete事件");
  181. }
  182. OnComplete.Invoke(returnObj, errorInfo);
  183. }
  184. private BusyControl BusyCtl = new BusyControl();
  185. /// <summary>
  186. /// 发送请求
  187. /// </summary>
  188. void DoWork(object sender, DoWorkEventArgs e)
  189. {
  190. if (ParentBusy != null)
  191. {
  192. ParentBusy.Invoke(new dlgAddControl(addCtl), ParentBusy, BusyCtl);
  193. }
  194. errorInfo = "";
  195. returnObj = _RemotingHelp.ExecuteMethod(CallingMessage, out errorInfo);
  196. }
  197. delegate void dlgAddControl(Control Pctl, BusyControl BusyCtl);
  198. private void addCtl(Control Pctl, BusyControl BusyCtl)
  199. {
  200. BusyCtl.Size = new System.Drawing.Size(32, 32);
  201. Pctl.Controls.Add(BusyCtl);
  202. BusyCtl.BringToFront();
  203. BusyCtl.Location = BusyLocation;
  204. BusyCtl.Start();
  205. }
  206. public void AsyncExecuteMethod(string serverName, string className, string methodName, object[] args)
  207. {
  208. //== 解析参数
  209. CallingMessage par = new CallingMessage();
  210. par.ServerName = serverName;
  211. par.ClassName = className;
  212. par.MethodName = methodName;
  213. par.args = args;
  214. par.ServerType = MesServerType.IComponentContainServer;
  215. par.TransType = NetWorkTransType.Remoting;
  216. par.visitType = VisitType.Method;
  217. AsyncExecuteMethod(par);
  218. }
  219. }
  220. #endregion
  221. }