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 void AfterReceiveReply(ref Message reply, object correlationState) { #if DEBUG var message = reply.ToString(); Console.WriteLine("Client got message:" + message); #endif ClientApplicationContext.Current = reply.GetApplicationContext <ClientApplicationContext>(); try { var setting = WcfSettingManager.CurrentClientSetting((correlationState as Type)); var logSetting = setting.WcfLogSetting; if (logSetting.Enabled && logSetting.MessageInfoSetting.Enabled) { var direct = logSetting.MessageInfoSetting.MessageDirection; if (direct == MessageDirection.Both || direct == MessageDirection.Receive) { var log = WcfLogProvider.GetClientMessageInfo( (correlationState as Type).FullName, ClientApplicationContext.Current.RequestIdentity, "ClientMessageInspector.AfterReceiveReply", MessageDirection.Receive, reply.ToString()); WcfServiceLocator.GetLogService().LogWithoutException(log); } } var securitySetting = setting.WcfSecuritySetting; if (securitySetting.PasswordCheck.Enable) { if (securitySetting.PasswordCheck.Direction == OperationDirection.Both || securitySetting.PasswordCheck.Direction == OperationDirection.Reply) { if (ClientApplicationContext.Current.Password != securitySetting.PasswordCheck.Password) { throw new WcfSecurityException("PasswordCheck failed in reply!"); } } } } catch (Exception ex) { LocalLogService.Log(ex.ToString()); if (ex is WcfSecurityException) { throw; } } }
public object BeforeSendRequest(ref Message request, IClientChannel channel) { #if DEBUG var message = request.ToString(); Console.WriteLine("Client send message:" + message); #endif try { var channelType = channel.GetType(); var serverContext = new ServerApplicationContext(); var clientContext = new ClientApplicationContext(); clientContext.RequestIdentity = serverContext.RequestIdentity; ClientApplicationContext.Current = clientContext; var setting = WcfSettingManager.CurrentClientSetting(channelType); var securitySetting = setting.WcfSecuritySetting; if (securitySetting.PasswordCheck.Enable) { if (securitySetting.PasswordCheck.Direction == OperationDirection.Both || securitySetting.PasswordCheck.Direction == OperationDirection.Request) { serverContext.Password = securitySetting.PasswordCheck.Password; } } var logSetting = setting.WcfLogSetting; if (logSetting.Enabled && logSetting.MessageInfoSetting.Enabled) { var direct = logSetting.MessageInfoSetting.MessageDirection; if (direct == MessageDirection.Both || direct == MessageDirection.Send) { var log = WcfLogProvider.GetClientMessageInfo( channelType.FullName, ClientApplicationContext.Current.RequestIdentity, "ClientMessageInspector.BeforeSendRequest", MessageDirection.Send, request.ToString()); WcfServiceLocator.GetLogService().LogWithoutException(log); } } serverContext.RequestIdentity = Guid.NewGuid().ToString(); serverContext.ClientMachineName = WcfLogProvider.MachineName; if (!contractVersionCache.ContainsKey(channelType.FullName)) { lock (locker) { if (!contractVersionCache.ContainsKey(channelType.FullName)) { contractVersionCache.Add(channelType.FullName, channelType.Assembly.GetName().Version.ToString()); } } } serverContext.ClientVersion = contractVersionCache[channelType.FullName]; request.SetApplicationContext(serverContext); return(channelType); } catch (Exception ex) { LocalLogService.Log(ex.ToString()); } return(channel.GetType()); }