Exemple #1
0
        public WcfClientSetting GetClientSetting(string serviceContractType, string machineIP)
        {
            var setting = new WcfClientSetting
            {
                WcfCoreSetting = new WcfClientCoreSetting
                {
                },
                WcfPerformanceServiceSetting = new WcfPerformanceServiceSetting
                {
                    Enabled = true,
                    ReportStateIntervalMilliSeconds = 10000,
                    AllowMethods = new List <string> {
                        "*"
                    },
                    DenyMethods = new List <string>(),
                },
                WcfLogSetting = new WcfLogSetting
                {
                    Enabled = true,
                    ExceptionInfoSetting = new ExceptionInfoSetting
                    {
                        Enabled = true,
                    },
                    InvokeInfoSetting = new InvokeInfoSetting
                    {
                        Enabled = false,
                    },
                    StartInfoSetting = new StartInfoSetting
                    {
                        Enabled = true,
                    },
                    MessageInfoSetting = new MessageInfoSetting
                    {
                        Enabled = false,
                    }
                },
                WcfSecuritySetting = new WcfSecuritySetting
                {
                    PasswordCheck = new PasswordCheck
                    {
                        Enable = false,
                    }
                }
            };

            try
            {
                var endpoints = WcfConfig.GetClientEndpoints().Where(ep => ep.ContractTypeName == serviceContractType).ToList();
                var e         = endpoints.Where(endpoint => machineIP.Contains(endpoint.ClientMachineIP) || endpoint.ClientMachineIP == "*").FirstOrDefault();
                if (e == null)
                {
                    Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetClientSetting", string.Format("没有获取到ClientEndpoint,参数为{0},{1}", serviceContractType, machineIP));
                }
                else
                {
                    var clientConfig = e.ClientSetting;
                    if (clientConfig != null)
                    {
                        setting.WcfCoreSetting = clientConfig.WcfCoreSetting;
                        setting.WcfLogSetting  = clientConfig.WcfLogSetting;
                    }
                    else
                    {
                        Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetClientSetting", string.Format("没有获取到ClientSetting,参数为{0},{1}", serviceContractType, machineIP));
                    }
                    var serviceEndpoint = WcfConfig.GetServiceEndpoints().Where(se => se.ContractTypeName == serviceContractType).FirstOrDefault();
                    if (serviceEndpoint != null)
                    {
                        var service = WcfConfig.GetServices().Where(s => s.ServiceTypeName == serviceEndpoint.ServiceTypeName).FirstOrDefault();
                        if (service != null)
                        {
                            var serviceConfig = service.ServerSetting;
                            if (serviceConfig != null)
                            {
                                setting.WcfSecuritySetting = serviceConfig.WcfSecuritySetting;
                            }
                            else
                            {
                                Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetClientSetting", string.Format("没有获取到ServerSetting,参数为{0},{1}", serviceContractType, machineIP));
                            }
                        }
                        else
                        {
                            Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetClientSetting", string.Format("没有获取到Service,参数为{0},{1}", serviceContractType, machineIP));
                        }
                    }
                    else
                    {
                        Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetClientSetting", string.Format("没有获取到ServiceEndpoint,参数为{0},{1}", serviceContractType, machineIP));
                    }
                }
            }
            catch (Exception ex)
            {
                Error("Adhesive.DistributedService.Config", "WcfConfigService.GetClientSetting", string.Format("GetClientSetting出错:{0},参数为{1},{2}", ex.ToString(), serviceContractType, machineIP));
            }

            return(setting);
        }
Exemple #2
0
        public WcfClientEndpointConfig GetWcfClientEndpoint(string serviceContractType, string serviceContractVersion, string machineIP)
        {
            try
            {
                var wcfClientEndpoint = (from ep in (WcfConfig.GetServiceEndpoints()
                                                     .Where(s => s.ContractTypeName == serviceContractType).ToList())
                                         let binding = WcfConfig.GetBindings().Single(b => b.BindingName == ep.ServiceEndpointBindingName)
                                                       where (ep.ContractVersion == "*" || float.Parse(ep.ContractVersion) >= float.Parse(serviceContractVersion))
                                                       orderby ep.ContractVersion ascending, binding.BindingPriority ascending
                                         select new WcfClientEndpointConfig
                {
                    EndpointName = ep.ServiceEndpointName,
                    EndpointPort = ep.ServiceEndpointPort,
                    ServiceContractType = ep.ContractTypeName,
                    EndpointBindingType = binding.BindingType,
                    EndpointBindingXml = binding.BindingXml != null ? binding.BindingXml.ToString() : "",
                    EndpointProtocol = binding.BindingProtocol,
                    ServiceType = ep.ServiceTypeName,
                }).FirstOrDefault();

                if (wcfClientEndpoint == null)
                {
                    Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfClientEndpoint", string.Format("GetWcfClientEndpoint没获取到ClientEndpoint信息,参数为{0},{1},{2}", serviceContractType, serviceContractVersion, machineIP));
                    return(null);
                }
                var wcfService = WcfConfig.GetServices().Where(s => s.ServiceTypeName == wcfClientEndpoint.ServiceType).FirstOrDefault();
                if (wcfService == null)
                {
                    Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfClientEndpoint", string.Format("GetWcfClientEndpoint没获取到Service信息,参数为{0},{1},{2}", serviceContractType, serviceContractVersion, machineIP));
                    return(null);
                }

                var wcfServerFarm = WcfConfig.GetServerFarms().FirstOrDefault(f => f.ServerFarmName == wcfService.ServerFarmName && machineIP.Contains(f.ClientMachineIP));
                if (wcfServerFarm == null)
                {
                    wcfServerFarm = WcfConfig.GetServerFarms().FirstOrDefault(f => f.ServerFarmName == wcfService.ServerFarmName && f.ClientMachineIP == "*");
                }
                if (wcfServerFarm == null)
                {
                    Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfClientEndpoint", string.Format("GetWcfClientEndpoint没获取到ServerFarm信息,参数为{0},{1},{2}", serviceContractType, serviceContractVersion, machineIP));
                    return(null);
                }
                wcfClientEndpoint.EndpointAddress = wcfServerFarm.ServerFarmAddress;
                wcfClientEndpoint.ServerFarmName  = wcfServerFarm.ServerFarmName;

                var allowFarmNames = WcfConfig.GetAllowClientAccesses().Where(acc => acc.ClientMachineIP == "*" || machineIP.Contains(acc.ClientMachineIP)).Select(a => a.AccessServerFarmName).ToList();
                if (!allowFarmNames.Contains("*") && !allowFarmNames.Contains(wcfClientEndpoint.ServerFarmName))
                {
                    Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfClientEndpoint", string.Format("GetWcfClientEndpoint没访问ServerFarm的权限,原因是不在允许列表中,参数为{0},{1},{2}", serviceContractType, serviceContractVersion, machineIP));
                    return(null);
                }

                var denyFarmNames = WcfConfig.GetDenyClientAccesses().Where(acc => acc.ClientMachineIP == "*" || machineIP.Contains(acc.ClientMachineIP)).Select(a => a.AccessServerFarmName).ToList();
                if (denyFarmNames.Contains("*") || denyFarmNames.Contains(wcfClientEndpoint.ServerFarmName))
                {
                    Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfClientEndpoint", string.Format("GetWcfClientEndpoint没访问ServerFarm的权限,原因是在拒绝列表中,参数为{0},{1},{2}", serviceContractType, serviceContractVersion, machineIP));
                    return(null);
                }

                var query =
                    (from ce in WcfConfig.GetClientEndpoints()
                     where ce.ContractTypeName == wcfClientEndpoint.ServiceContractType
                     select ce.ClientEndpointBehaviorXml).FirstOrDefault();

                wcfClientEndpoint.EndpointBehaviorXml = query != null?query.ToString() : "";

                return(wcfClientEndpoint);
            }
            catch (Exception ex)
            {
                Error("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfClientEndpoint", string.Format("GetWcfClientEndpoint出错:{0},参数为{1},{2},{3}", ex.ToString(), serviceContractType, serviceContractVersion, machineIP));
                return(null);
            }
        }