internal static WcfChannelWrapper <T> CreateServiceClient <T>() where T : class { string typeName = typeof(T).FullName; ChannelFactory cf; if (!channelFactoryCache.TryGetValue(typeName, out cf)) { lock (cacheLocker) { if (!channelFactoryCache.TryGetValue(typeName, out cf)) { if (typeof(T) == typeof(IWcfConfigService)) { var configServiceAddress = ConfigurationManager.AppSettings[WCFCONFIGSERVICE_ADDRESS_KEY]; if (string.IsNullOrEmpty(configServiceAddress)) { throw new Exception("Please set [" + WCFCONFIGSERVICE_ADDRESS_KEY + "] appSettings in your config!"); } var binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None; var address = string.Format("net.tcp://{0}", configServiceAddress); cf = new ChannelFactory <IWcfConfigService>(binding, address); } else if (typeof(T) == typeof(IWcfLogService)) { var logServiceAddress = ConfigurationManager.AppSettings[WCFLOGSERVICE_ADDRESS_KEY]; if (string.IsNullOrEmpty(logServiceAddress)) { throw new Exception("Please set [" + WCFLOGSERVICE_ADDRESS_KEY + "] appSettings in your config!"); } var elements = new List <BindingElement>(); elements.Add(new BinaryMessageEncodingBindingElement()); elements.Add(new TcpTransportBindingElement()); var binding = new CustomBinding(elements); var address = string.Format("net.tcp://{0}", logServiceAddress); cf = new ChannelFactory <IWcfLogService>(binding, address); } else { var endpoint = GetWcfClientEndpointConfiguration(typeof(T)); cf = CreateChannelFactory <T>(endpoint); WcfSettingManager.Init <T>(); ConfigUpdateManager.InitClient(); if (WcfSettingManager.CurrentClientSetting <T>().WcfLogSetting.StartInfoSetting.Enabled) { var log = WcfLogProvider.GetClientStartInfo(typeof(T).FullName, "WcfServiceClientFactory.CreateServiceClient", endpoint); WcfServiceLocator.GetLogService().LogWithoutException(log); } } channelFactoryCache[typeName] = cf; } } } return(new WcfChannelWrapper <T>((cf as ChannelFactory <T>).CreateChannel())); }
public static ServiceHost CreateServiceHost(Type serviceType) { string typeName = serviceType.FullName; var serviceHost = new ServiceHost(serviceType); if (!typeof(IWcfConfigService).IsAssignableFrom(serviceType) && !typeof(IWcfLogService).IsAssignableFrom(serviceType)) { WcfSettingManager.Init(serviceType); if (serviceHost.Description.Behaviors.Find <ServiceErrorBehavior>() == null) { serviceHost.Description.Behaviors.Add(new ServiceErrorBehavior()); } if (serviceHost.Description.Behaviors.Find <ActionInterceptBehavior>() == null) { serviceHost.Description.Behaviors.Add(new ActionInterceptBehavior()); } if (WcfSettingManager.CurrentServerSetting(serviceType).WcfCoreSetting.EnableUnity) { if (serviceHost.Description.Behaviors.Find <UnityServiceBehavior>() == null) { serviceHost.Description.Behaviors.Add(new UnityServiceBehavior()); } } serviceHost.Description.Endpoints.Clear(); var wcfService = GetWcfServiceConfiguration(serviceType); if (wcfService == null) { throw new Exception("Can not locate any Wcf service, please check metadata config!"); } var bindingCache = new Dictionary <string, Binding>(); foreach (var ep in wcfService.Endpoints) { string address = ConfigHelper.CreateAddress( ep.EndpointProtocol, Environment.MachineName, ep.EndpointPort, ep.EndpointName); Binding binding; if (!bindingCache.TryGetValue(address, out binding)) { binding = ConfigHelper.CreateBinding(ep.EndpointBindingType, ep.EndpointBindingXml); bindingCache[address] = binding; } serviceHost.AddServiceEndpoint(ep.ServiceContractType, binding, address); if (!string.IsNullOrEmpty(ep.EndpointBehaviorXml)) { AddEndPointBehavior(serviceHost.Description.Endpoints.Last(), ep.EndpointBehaviorXml); } } foreach (var ep in serviceHost.Description.Endpoints) { ep.Behaviors.Add(new MessageInspectorEndpointBehavior()); } if (!string.IsNullOrEmpty(wcfService.ServiceBehaviorXml)) { AddServiceBehavior(serviceHost, wcfService.ServiceBehaviorXml); } serviceHost.Opened += (sender, o) => { if (WcfSettingManager.CurrentServerSetting(serviceType).WcfLogSetting.StartInfoSetting.Enabled) { var log = WcfLogProvider.GetServerStartInfo(serviceType.FullName, "WcfServiceHostFactory.CreateServiceHost", wcfService); WcfServiceLocator.GetLogService().LogWithoutException(log); } }; } return(serviceHost); }