/// <summary> /// 初始化Web /// </summary> public static void InitWeb() { if (_current != null && _current.ServiceName != NULL) { return; } lock (_syncRoot) { string runMode = WebConfigurationManager.AppSettings["RunMode"]; string centerUrl = WebConfigurationManager.AppSettings["CenterUrl"]; string serviceName = WebConfigurationManager.AppSettings["ServiceName"]; string computerName = WebConfigurationManager.AppSettings["ComputerName"] ?? Environment.MachineName; if (runMode == "LocalWeb") { _current = new ServiceSettingsConfigProxy(serviceName); IICConfigurationManager.Loader = new LocalConfigurationLoader(); _current.UpdateConfig(ServiceRunMode.LocalWeb, null); RpcProxyFactory.RegisterClientChannel(new RpcPipeClientChannel()); TracingManager.UpdateConfig(); } else { _current = new ServiceSettingsConfigProxy(serviceName, computerName); RpcProxyFactory.RegisterClientChannel(new RpcHttpClientChannel()); RpcProxyFactory.RegisterClientChannel(new RpcPipeClientChannel()); HAConfigurationLoader loader = new HAConfigurationLoader(serviceName, computerName, centerUrl); HAServiceSettings settings = loader.LoadServiceSettings(); IICConfigurationManager.Loader = loader; _current.UpdateConfig(ServiceRunMode.HAWeb, settings); TracingManager.UpdateConfig(); } } }
/// <summary> /// 使用HA方式初始化服务 /// </summary> /// <param name="serviceName">服务名</param> /// <param name="computerName">获取配置使用的计算机名</param> /// <param name="centerUrl">HACenter的Url</param> public static void InitServiceHa(string serviceName, string computerName, string centerUrl) { if (_current != null && _current.ServiceName != NULL) { return; } lock (_syncRoot) { _current = new ServiceSettingsConfigProxy(serviceName, computerName); RpcHttpClientChannel channel = new RpcHttpClientChannel(); RpcProxyFactory.RegisterClientChannel(channel); HAGetConfigArgs args = new HAGetConfigArgs(); args.ServiceName = serviceName; args.ComputerName = computerName; var proxy = RpcProxyFactory.GetProxy <IHACenterConfigService>(centerUrl); HAServiceSettings settings = proxy.Invoke <HAGetConfigArgs, HAServiceSettings>( "LoadServiceSettings", args); IICConfigurationManager.Loader = new HAConfigurationLoader(serviceName, computerName, centerUrl); _current.UpdateConfig(ServiceRunMode.HAService, settings); TracingManager.UpdateConfig(); } }
public void UpdateConfig(ServiceRunMode runMode, HAServiceSettings serviceSettings) { // // 不允许更新 _runMode = runMode; _section = IICConfigurationManager.Configurator.GetConfigSecion <ServiceSettingsConfigSection>( "ServiceSettings", null); // // 优先使用本地配置 _domain = _section.Domain; _site = _section.Site; int.TryParse(_section.PoolID, out _poolId); _pools = new List <int>(); foreach (string a in _section.Pools.Split(',')) { int p; if (int.TryParse(a, out p)) { _pools.Add(p); } } // // HA模式下, 如果ServiceSettings没有配置, 会使用来自HA_Computer与HA_Deployment的配置覆盖 // Pool, Site, Domain三项配置 switch (_runMode) { case ServiceRunMode.LocalWeb: case ServiceRunMode.LocalService: if (string.IsNullOrEmpty(_serviceRoleName)) { _serviceRoleName = _section.ServiceName; } break; case ServiceRunMode.HAService: case ServiceRunMode.HAWeb: // // 在HA方式下运行的服务, 可以通过ServiceSettings配置节, 替换ServiceSettings的配置 if (serviceSettings != null) { _serviceRoleName = serviceSettings.ServiceOriginName; if (string.IsNullOrEmpty(_domain)) { _domain = serviceSettings.Domain; } if (string.IsNullOrEmpty(_site)) { _site = serviceSettings.Site; } if (string.IsNullOrEmpty(_section.PoolID)) { _poolId = serviceSettings.PoolId; } } break; default: throw new NotSupportedException("Unexcepted RunMode:" + _runMode); } }