private static MessageLogConfig AttributeToConfig(MessageSensitivityAttribute attribute) { MessageLogConfig config = new MessageLogConfig(); if (attribute == null) { return(config); } switch (attribute.RequestSensitivity) { case SensitivityMode.Default: config.IsRequestSensitive = null; break; case SensitivityMode.Insensitive: config.IsRequestSensitive = false; break; case SensitivityMode.Sensitive: config.IsRequestSensitive = true; break; } switch (attribute.ResponseSensitivity) { case SensitivityMode.Default: config.IsResponseSensitive = null; break; case SensitivityMode.Insensitive: config.IsResponseSensitive = false; break; case SensitivityMode.Sensitive: config.IsResponseSensitive = true; break; } if (attribute.LogResponse) { config.LogResponse = attribute.LogResponse; } if (attribute.DisableLog) { config.DisableLog = attribute.DisableLog; } return(config); }
public ServiceMetadata(Type serviceType, string serviceName, string serviceNamespace, string codeGeneratorVersion) { this.RequestTypes = new HashSet <Type>(); this.ResponseTypes = new HashSet <Type>(); this.OperationNameMap = new Dictionary <string, Operation>(); this.Routes = new ServiceRoutes(); this.ServiceTypes = new List <Type>() { serviceType }; this.ServiceName = string.IsNullOrWhiteSpace(serviceName) ? AnonymousServiceName : serviceName; this.ServiceNamespace = string.IsNullOrWhiteSpace(serviceNamespace) ? "http://soa.ant.com/anonymous" : serviceNamespace; this.FullServiceName = ServiceName + "{" + ServiceNamespace + "}"; this.RefinedFullServiceName = ServiceUtils.RefineServiceName(ServiceNamespace, ServiceName); this.ServiceMetricPrefix = "soa.service"; this.AntServiceStackVersion = typeof(ServiceMetadata).Assembly.GetName().Version.ToString(); this.AntCodeGenVersion = codeGeneratorVersion; //判断appseting里面是否设置了特殊的访问前缀的 key //如果没有就默认为空的 string servicePathSettingValue = ConfigUtils.GetNullableAppSetting(GetServiceSpecificSettingKey(DefaultServicePathMapSettingKey)); this.IsDefaultService = string.IsNullOrWhiteSpace(servicePathSettingValue); if (this.IsDefaultService) { this.ServicePath = DefaultServicePath; } else { string servicePath = servicePathSettingValue.Trim().ToLower(); if (!Regex.IsMatch(servicePath, ServicePathPattern)) { throw new Exception("ServicePathMap setting is invalid: " + servicePathSettingValue); } this.ServicePath = servicePath; } //判断appseting里面是否设置了方法执行的timeout string operationTimeoutMapSettingKey = GetServiceSpecificSettingKey(DefaultOperationTimeoutMapSettingKey); var opTimeoutMapSetting = ConfigUtils.GetNullableAppSetting(operationTimeoutMapSettingKey); this.OperationTimeoutMap = ConfigUtils.GetDictionaryFromAppSettingValue(opTimeoutMapSetting); foreach (var item in DefaultOperationTimeoutMap) { if (!this.OperationTimeoutMap.ContainsKey(item.Key)) { this.OperationTimeoutMap[item.Key] = item.Value; } } #region 电容器开关 string circuitBreakerForceClosedSettingKey = GetServiceSpecificSettingKey(DefaultCircuitBreakerForceClosedSettingKey); bool circuitBreakerForceClosed; if (!bool.TryParse(ConfigUtils.GetNullableAppSetting(circuitBreakerForceClosedSettingKey), out circuitBreakerForceClosed)) { circuitBreakerForceClosed = DefaultCircuitBreakerForceClosed; } this.CircuitBreakerForceClosed = circuitBreakerForceClosed; #endregion string logErrorWithRequestInfoSettingKey = GetServiceSpecificSettingKey(DefaultLogErrorWithRequestInfoSettingKey); bool logErrorWithRequestInfo; if (!bool.TryParse(ConfigUtils.GetNullableAppSetting(logErrorWithRequestInfoSettingKey), out logErrorWithRequestInfo)) { logErrorWithRequestInfo = DefaultLogErrorWithRequestInfo; } this.LogErrorWithRequestInfo = logErrorWithRequestInfo; string logH5HeadExtensionDataSettingKey = GetServiceSpecificSettingKey(DefaultLogH5HeadExtensionDataSettingKey); bool logH5HeadExtensionData; if (!bool.TryParse(ConfigUtils.GetNullableAppSetting(logH5HeadExtensionDataSettingKey), out logH5HeadExtensionData)) { logH5HeadExtensionData = DefaultLogH5HeadExtensionData; } this.LogH5HeadExtensionData = logH5HeadExtensionData; string logCommonRequestInfoSettingKey = GetServiceSpecificSettingKey(DefaultLogCommonRequestInfoSettingKey); bool logCommonRequestInfo; if (!bool.TryParse(ConfigUtils.GetNullableAppSetting(logCommonRequestInfoSettingKey), out logCommonRequestInfo)) { logCommonRequestInfo = DefaultLogCommonRequestInfo; } this.LogCommonRequestInfo = logCommonRequestInfo; string useChunkedTransferEncodingSettingKey = GetServiceSpecificSettingKey(DefaultUseChunkedTransferEncodingSettingKey); bool useChunkedTransferEncoding; if (!bool.TryParse(ConfigUtils.GetNullableAppSetting(useChunkedTransferEncodingSettingKey), out useChunkedTransferEncoding)) { useChunkedTransferEncoding = DefaultUseChunkedTransferEncoding; } this.UseChunkedTransferEncoding = useChunkedTransferEncoding; string serviceTestSubEnvSettingKey = GetServiceSpecificSettingKey(SERVICE_REGISTRY_ENV_KEY); ServiceTestSubEnv = ConfigUtils.GetNullableAppSetting(serviceTestSubEnvSettingKey); if (string.IsNullOrWhiteSpace(ServiceTestSubEnv)) { ServiceTestSubEnv = DefaultServiceTestSubEnv; } else { ServiceTestSubEnv = ServiceTestSubEnv.Trim().ToLower(); } #region 对于每个单个IP的连接数限制 //是否打开开关的配置 string checkConnectionMaxRequestCountSettingKey = GetServiceSpecificSettingKey(DefaultCheckConnectionMaxRequestCountSettingKey); bool checkConnectionMaxRequestCount; if (!bool.TryParse(ConfigUtils.GetNullableAppSetting(checkConnectionMaxRequestCountSettingKey), out checkConnectionMaxRequestCount)) { checkConnectionMaxRequestCount = DefaultCheckConnectionMaxRequestCount;//默认false } this.CheckConnectionMaxRequestCount = checkConnectionMaxRequestCount; string connectionMaxRequestCountSettingKey = GetServiceSpecificSettingKey(DefaultConnectionMaxRequestCountSettingKey); int connectionMaxRequestCount; if (!int.TryParse(ConfigUtils.GetNullableAppSetting(connectionMaxRequestCountSettingKey), out connectionMaxRequestCount)) { connectionMaxRequestCount = DefaultConnectionMaxRequestCount; // 如果没有配置就默认 2000 } else if (connectionMaxRequestCount < MinConnectionMaxRequestCount) { connectionMaxRequestCount = MinConnectionMaxRequestCount;//如果配置了低于500 就取500 (最低不能低于500) } this.ConnectionMaxRequestCount = connectionMaxRequestCount; #endregion string deserializeRequestUseMemoryStreamSettingKey = GetServiceSpecificSettingKey(DefaultDeserializeRequestUseMemoryStreamSettingKey); bool deserializeRequestUseMemoryStream; if (!bool.TryParse(ConfigUtils.GetNullableAppSetting(deserializeRequestUseMemoryStreamSettingKey), out deserializeRequestUseMemoryStream)) { deserializeRequestUseMemoryStream = DefaultDeserializeRequestUseMemoryStream; } this.DeserializeRequestUseMemoryStream = deserializeRequestUseMemoryStream; string metadataFeatureEnabledSettingKey = GetServiceSpecificSettingKey(DefaultEnableMetadataFeatureConfigKey); bool metadataFeatureEnabled; if (!bool.TryParse(ConfigUtils.GetNullableAppSetting(metadataFeatureEnabledSettingKey), out metadataFeatureEnabled)) { metadataFeatureEnabled = DefaultMetadataFeatureEnabled; } this.MetadataFeatureEnabled = metadataFeatureEnabled; MessageSensitivityAttribute sensitivityAttribute = null; if (serviceType != null) { sensitivityAttribute = serviceType.GetCustomAttributes(true).OfType <MessageSensitivityAttribute>().FirstOrDefault(); } this.ServiceMessageLogConfig = AttributeToConfig(sensitivityAttribute); }