private RemotingClientUtil() { _RemotingConfiguration = RemotingClientConfiguration.GetConfig(); if (_RemotingConfiguration == null) { return; } Dictionary <string, RemotingHost> hosts = _RemotingConfiguration.RemotingHosts; //生成所有远程对象代理并加载到内存 foreach (KeyValuePair <string, RemotingHost> kvp in hosts) { RemotingHost host = kvp.Value; LoadModulesByHost(host); } //检测每个客户端的可用服务器 RemotingHostCheck.Instance.DoCheck(); //如果检测服务器,则输出日志 if (_RemotingConfiguration.IsCheckServer) { System.Threading.Thread thread = new System.Threading.Thread(DoWork); //thread.IsBackground = true; thread.Start(); } }
public RemotingServer() { _domain = AppDomain.CreateDomain("server", null, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory }); _remotingHost = (RemotingHost)_domain.CreateInstanceAndUnwrap(typeof(RemotingHost).Assembly.FullName, typeof(RemotingHost).FullName); _remotingHost.Start(); }
/// <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()); }
/// <summary> /// 加载远程对象代理客户端模块 /// </summary> /// <param name="host"></param> public void LoadModulesByHost(RemotingHost host) { string serverUrl = RemotingHostCheck.Instance.GetUsableServerUrl(host); foreach (KeyValuePair <string, string> m in host.Modules) { string objectUrl = _RemotingConfiguration.GetRemoteObjectUrl(serverUrl, m.Value); T instance = (T)Activator.GetObject(typeof(T), objectUrl); string key = string.Format("{0}${1}${2}", host.Name, serverUrl, m.Key); _RemoteObjects[key] = instance; } }
/// <summary> /// 获取远程对象 /// </summary> /// <param name="hostName"></param> /// <param name="remoteObjectName"></param> /// <returns></returns> public T GetRemotingObject(string hostName, string remoteObjectName) { RemotingHost host = _RemotingConfiguration.RemotingHosts[hostName]; string serverUrl = RemotingHostCheck.Instance.GetUsableServerUrl(host); string key = string.Format("{0}${1}${2}", host.Name, serverUrl, remoteObjectName); lock (_RemoteObjects) { if (!_RemoteObjects.ContainsKey(key)) { string objectUrl = _RemotingConfiguration.GetRemoteObjectUrl(serverUrl, remoteObjectName); T instance = (T)Activator.GetObject(typeof(T), objectUrl); _RemoteObjects[key] = instance; } } return(_RemoteObjects[key]); }
/// <summary> /// 获取远程对象(默认为第一个RemotingClient的默认服务器) /// </summary> /// <param name="remoteObjectName"></param> /// <returns></returns> public T GetRemotingObject(string remoteObjectName) { RemotingHost host = null; foreach (KeyValuePair <string, RemotingHost> kvp in _RemotingConfiguration.RemotingHosts) { host = kvp.Value; break; } if (host != null) { return(GetRemotingObject(host.Name, remoteObjectName)); } else { return(default(T)); } }
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; }