Esempio n. 1
0
        public WcfServiceConfig GetWcfService(string serviceType, string serviceContractVersion, string machineIP)
        {
            try
            {
                var wcfServices = WcfConfig.GetServices().Where(s => s.ServiceTypeName == serviceType).ToList();
                var wcfService  = wcfServices.Where(s => machineIP.Contains(s.ServerMachineIP) || s.ServerMachineIP == "*").FirstOrDefault();
                if (wcfService == null)
                {
                    Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfService", string.Format("GetWcfService没获取到Service信息,参数为{0},{1},{2}", serviceType, serviceContractVersion, machineIP));
                    return(null);
                }

                var service = new WcfServiceConfig
                {
                    ServiceType        = serviceType,
                    ServiceBehaviorXml = wcfService.ServiceBehaviorXml != null?wcfService.ServiceBehaviorXml.ToString() : "",
                };

                var endpoints = (from ep in WcfConfig.GetServiceEndpoints()
                                 where ep.ServiceTypeName == serviceType &&
                                 (ep.ContractVersion == serviceContractVersion || ep.ContractVersion == "*") &&
                                 (ep.ServerMachineIP == wcfService.ServerMachineIP || ep.ServerMachineIP == "*")
                                 select new WcfServiceEndpointConfig
                {
                    EndpointBehaviorXml = ep.ServiceEndpointBehaviorXml != null ? ep.ServiceEndpointBehaviorXml.ToString() : "",
                    EndpointBindingName = ep.ServiceEndpointBindingName,
                    EndpointName = ep.ServiceEndpointName,
                    EndpointPort = ep.ServiceEndpointPort,
                    ServiceContractType = ep.ContractTypeName
                }).ToList();

                if (endpoints == null)
                {
                    Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfService", string.Format("GetWcfService没获取到Endpoint信息,参数为{0},{1},{2}", serviceType, serviceContractVersion, machineIP));
                    return(null);
                }
                service.Endpoints = endpoints;
                foreach (var endpoint in service.Endpoints)
                {
                    var binding = WcfConfig.GetBindings().Single(b => b.BindingName == endpoint.EndpointBindingName);
                    if (binding == null)
                    {
                        Warning("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfService", string.Format("GetWcfService没获取到Binding信息,参数为{0},{1},{2}", serviceType, serviceContractVersion, machineIP));
                        return(null);
                    }
                    else
                    {
                        endpoint.EndpointBindingType = binding.BindingType;
                        endpoint.EndpointBindingXml  = binding.BindingXml != null?binding.BindingXml.ToString() : "";

                        endpoint.EndpointProtocol = binding.BindingProtocol;
                    }
                }

                return(service);
            }
            catch (Exception ex)
            {
                Error("Adhesive.DistributedService.Config", "WcfConfigService.GetWcfService", string.Format("GetWcfService出错:{0},参数为{1},{2},{3}", ex.ToString(), serviceType, serviceContractVersion, machineIP));
                return(null);
            }
        }
Esempio n. 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);
            }
        }