using System; using System.Data; using System.Collections; using System.Windows.Forms; using Core.Mes.IBaseInterface; using System.Drawing; using System.ComponentModel; namespace Core.Mes.ClientFrameWork { /// /// RemotingHelp 的摘要说明。 /// public class RemotingHelp { public RemotingHelp() { } public DataSet ServerUrlList { set { DataSet ds = value; if (ds != null || ds.Tables.Count > 0) { _htServerUrlList = new Hashtable(); foreach (DataRow dr in ds.Tables[0].Rows) { _htServerUrlList.Add(dr["ServerName"].ToString(), dr["Url"].ToString() + "/" + dr["ServerName"]); } } ds.Dispose(); } } private Hashtable _htServerUrlList = new Hashtable(); private Hashtable _htRemoteServer = new Hashtable(); private bool GetServer(string serverName) { try { if (!_htRemoteServer.Contains(serverName)) _htRemoteServer.Add(serverName, (ICommon)Activator.GetObject(typeof(ICommon), _htServerUrlList[serverName].ToString())); } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } return true; } public object ExecuteMethod(CallingMessage par, out string errorInfo) { errorInfo = ""; if (!FindRemoteServer(par.ServerName)) { errorInfo = "未找到请求的服务!"; return null; } try { ReturnObject rtnObj = (_htRemoteServer[par.ServerName] as ICommon).MethodHandler(par, new ValidateInfo()); errorInfo = rtnObj.ErrMessage; return rtnObj.RealObject; } catch (Exception ex) { errorInfo = ex.Message; return null; } } public object ExecuteMethod(string serverName, string className, string methodName, object[] args, out string errorInfo) { //== 解析参数 CallingMessage par = new CallingMessage(); par.ServerName = serverName; par.ClassName = className; par.MethodName = methodName; par.args = args; par.ServerType = MesServerType.IComponentContainServer; par.TransType = NetWorkTransType.Remoting; par.visitType = VisitType.Method; errorInfo = ""; return ExecuteMethod(par, out errorInfo); } private bool FindRemoteServer(string serverName) { if (!_htRemoteServer.Contains(serverName)) return GetServerUrl(serverName); return true; } private bool GetServerUrl(string serverName) { if (!_htServerUrlList.Contains(serverName)) return false; return GetServer(serverName); } public void InitServerUrlList(string serverName, string url) { if (_htServerUrlList.Count == 0) _htServerUrlList.Add(serverName, url); } public AsyncExecute AsyncExecute { get { return new AsyncExecute(this); } } } #region add maliang 2012-4-12 public class AsyncExecute { private RemotingHelp _RemotingHelp = null; public AsyncExecute(RemotingHelp pRemotingHelp) { _RemotingHelp = pRemotingHelp; } private Form m_ParentBusy; /// /// 需要显示忙的窗体。 /// public Form ParentBusy { get { return m_ParentBusy; } set { m_ParentBusy = value; } } private Point m_BusyLocation = new Point(0, 0); /// /// 需要显示忙的窗体。 /// public Point BusyLocation { get { return m_BusyLocation; } set { m_BusyLocation = value; } } /// /// 请求完成的委托 /// /// 返回的对象 /// 返回的消息内容 public delegate void CompleteEventHandler(object returnObj, string returnMsg); /// /// 请求完成的事件,如果返回的对象需要处理,请订阅该事件 /// public event CompleteEventHandler OnComplete; /// /// 参数 /// private CallingMessage CallingMessage; /// /// 错误信息 /// private string errorInfo = string.Empty; /// /// 返回的对象 /// private object returnObj; public void AsyncExecuteMethod(CallingMessage par) { CallingMessage = par; BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler(DoWork); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted); bw.WorkerReportsProgress = true; bw.RunWorkerAsync(); } /// /// 请求完成 /// void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (ParentBusy != null) { BusyCtl.Stop(); ParentBusy.Controls.Remove(BusyCtl); } string msg = string.Empty; //如果订阅了该事件并且返回的结果没有问题才调用完成的事件 if (OnComplete == null) { throw new Exception("调用接口请求时未订阅OnComplete事件,请在调用AsyncExecuteMethod请求对象是订阅OnComplete事件"); } OnComplete.Invoke(returnObj, errorInfo); } private BusyControl BusyCtl = new BusyControl(); /// /// 发送请求 /// void DoWork(object sender, DoWorkEventArgs e) { if (ParentBusy != null) { ParentBusy.Invoke(new dlgAddControl(addCtl), ParentBusy, BusyCtl); } errorInfo = ""; returnObj = _RemotingHelp.ExecuteMethod(CallingMessage, out errorInfo); } delegate void dlgAddControl(Control Pctl, BusyControl BusyCtl); private void addCtl(Control Pctl, BusyControl BusyCtl) { BusyCtl.Size = new System.Drawing.Size(32, 32); Pctl.Controls.Add(BusyCtl); BusyCtl.BringToFront(); BusyCtl.Location = BusyLocation; BusyCtl.Start(); } public void AsyncExecuteMethod(string serverName, string className, string methodName, object[] args) { //== 解析参数 CallingMessage par = new CallingMessage(); par.ServerName = serverName; par.ClassName = className; par.MethodName = methodName; par.args = args; par.ServerType = MesServerType.IComponentContainServer; par.TransType = NetWorkTransType.Remoting; par.visitType = VisitType.Method; AsyncExecuteMethod(par); } } #endregion }