/// <summary> /// 获取可用的服务器地址 /// </summary> /// <param name="host"></param> /// <returns></returns> public string GetUsableServerUrl(RemotingHost host) { if (AppDomain.CurrentDomain.GetData(host.Name) == null) { string url = host.Servers[host.DefaultServer].ServerUrl; AppDomain.CurrentDomain.SetData(host.Name, url); } return AppDomain.CurrentDomain.GetData(host.Name).ToString(); }
void CheckRemotingHost(RemotingHost host) { string objectUrl = string.Empty; RemotingServer defaultServer = host.Servers[host.DefaultServer]; string usableServerUrl = this.GetUsableServerUrl(host); bool flag = false; //是否需要重设当前可用服务器标志 host.IsChecking = true; //首先检查当前可用服务器 try { objectUrl = cfg.GetRemoteObjectUrl(usableServerUrl, "RemotingTest"); IRemotingTest t = (IRemotingTest)Activator.GetObject(typeof(RemotingTest), objectUrl); t.GetDate(); WriteLog(host.Name, usableServerUrl, true, "ok"); } catch (Exception ex) { flag = true; //需要重设当前可用服务器标志 WriteLog(host.Name, usableServerUrl, false, ex.Message); } //若当前可用服务器不是默认服务器,则再检查默认服务器,若其可用,则还原 if (defaultServer.ServerUrl != usableServerUrl) { try { objectUrl = cfg.GetRemoteObjectUrl(defaultServer.ServerUrl, "RemotingTest"); IRemotingTest t = (IRemotingTest)Activator.GetObject(typeof(RemotingTest), objectUrl); t.GetDate(); AppDomain.CurrentDomain.SetData(host.Name, defaultServer.ServerUrl); WriteLog(host.Name, defaultServer.ServerUrl, true, "ok"); } catch (Exception ex) { WriteLog(host.Name, defaultServer.ServerUrl, false, ex.Message); } } string serverUrl = string.Empty; //遍历其他服务器,检查其状态 foreach (KeyValuePair<string, RemotingServer> kvp in host.Servers) { serverUrl = kvp.Value.ServerUrl; if (serverUrl == usableServerUrl) continue; if (serverUrl == defaultServer.ServerUrl) continue; objectUrl = cfg.GetRemoteObjectUrl(serverUrl, "RemotingTest"); try { IRemotingTest t = (IRemotingTest)Activator.GetObject(typeof(RemotingTest), objectUrl); t.GetDate(); if (flag) { AppDomain.CurrentDomain.SetData(host.Name, serverUrl); //重设当前可用服务器 flag = false; WriteLog(host.Name, serverUrl, true, "ok"); } WriteLog(host.Name, serverUrl, false, "ok"); } catch (Exception ex) { WriteLog(host.Name, serverUrl, false, ex.Message); } } host.IsChecking = false; }
/// <summary> /// 从配置文件加载配置值 /// </summary> /// <param name="node"></param> public void LoadValuesFromConfigurationXml(XmlNode node) { if (node == null) { return; } if (node.Attributes["isCheckServer"].Value != null) { this._IsCheckServer = node.Attributes["isCheckServer"].Value == "true" ? true : false; } if (node.Attributes["interval"].Value != null) { this._Interval = Convert.ToDouble(node.Attributes["interval"].Value); } foreach (XmlNode n in node.ChildNodes) { if (n.NodeType == XmlNodeType.Comment) { continue; } if (n.Name == "remotingHost") { XmlAttributeCollection ac = n.Attributes; RemotingHost host = new RemotingHost(); host.Name = ac["name"].Value; host.DefaultServer = ac["defaultServer"].Value; Dictionary <string, string> modules = new Dictionary <string, string>(); Dictionary <string, RemotingServer> servers = new Dictionary <string, RemotingServer>(); foreach (XmlNode n1 in n.ChildNodes) { if (n1.NodeType == XmlNodeType.Comment) { continue; } XmlAttributeCollection ac1 = n1.Attributes; if (n1.Name == "server") { RemotingServer server = new RemotingServer(); server.ServerName = ac1["name"].Value; server.ServerUrl = ac1["url"].Value; servers.Add(server.ServerName, server); } if (n1.Name == "remoteObject") { string name = ac1["name"].Value; string objectUri = ac1["objectUri"].Value; if (!modules.ContainsKey(name)) { modules.Add(name, objectUri); } } }//end foreach host.Servers = servers; host.Modules = modules; if (!this._RemotingHosts.ContainsKey(host.Name)) { this._RemotingHosts.Add(host.Name, host); } } } }