public bool HandleError(Exception error) { try { if (OperationContext.Current.GetCurrentServiceDescription() != null) { var logSetting = WcfSettingManager.CurrentServerSetting(OperationContext.Current.GetCurrentServiceDescription().ServiceType).WcfLogSetting; if (logSetting.Enabled && logSetting.ExceptionInfoSetting.Enabled) { var exceptionID = ""; if (error.Data.Contains("id")) { exceptionID = error.Data["id"].ToString(); } ((ExceptionService)AppInfoCenterService.ExceptionService).WcfUnhandledServerException(error, OperationContext.Current.GetCurrentServiceDescription().ServiceType.FullName, exceptionID, ServerApplicationContext.Current.RequestIdentity); } } } catch (Exception ex) { ex.Handle(WcfLogProvider.ModuleName, "ErrorHandler", "HandleError"); } return(true); }
public void BeforeSendReply(ref Message reply, object correlationState) { try { var clientContext = new ClientApplicationContext(); clientContext.ServerMachineIP = CommonConfiguration.MachineIP; clientContext.RequestIdentity = ServerApplicationContext.Current.RequestIdentity; clientContext.ServerExceptionID = ServerApplicationContext.Current.ServerExceptionID ?? ""; var serviceType = OperationContext.Current.GetCurrentServiceDescription().ServiceType; if (!serviceVersionCache.ContainsKey(serviceType.FullName)) { lock (locker) { if (!serviceVersionCache.ContainsKey(serviceType.FullName)) { serviceVersionCache.Add(serviceType.FullName, serviceType.Assembly.GetName().Version.ToString()); } } } clientContext.ServerVersion = serviceVersionCache[serviceType.FullName]; reply.SetApplicationContext(clientContext); var setting = WcfSettingManager.CurrentServerSetting(OperationContext.Current.GetCurrentServiceDescription().ServiceType); var securitySetting = setting.WcfSecuritySetting; if (securitySetting.PasswordCheck.Enable) { if (securitySetting.PasswordCheck.Direction == OperationDirection.Both || securitySetting.PasswordCheck.Direction == OperationDirection.Reply) { clientContext.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.GetServerMessageInfo( "ServerMessageInspector.BeforeSendReply", MessageDirection.Send, reply.ToString()); MongodbService.MongodbInsertService.Insert(log); } } } catch (Exception ex) { ex.Handle(WcfLogProvider.ModuleName, "ServerMessageInspector", "BeforeSendReply"); } }
internal static WcfChannelWrapper <T> CreateServiceClient <T>() where T : class { try { 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 = CommonConfiguration.GetConfig().WcfConfigServiceAddress; 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(IConfigServer)) { var configServiceAddress = CommonConfiguration.GetConfig().ConfigServiceAddress; var binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None; binding.MaxReceivedMessageSize = 6553600; //var address = string.Format("net.tcp://{0}", configServiceAddress); cf = new ChannelFactory <IConfigServer>(binding, configServiceAddress); } else { var endpoint = GetWcfClientEndpointConfiguration(typeof(T)); cf = CreateChannelFactory <T>(endpoint); WcfSettingManager.Init <T>(); } if (cf != null) { channelFactoryCache[typeName] = cf; } } } } return(new WcfChannelWrapper <T>((cf as ChannelFactory <T>).CreateChannel())); } catch (Exception ex) { ex.Handle(WcfLogProvider.ModuleName, "WcfServiceClientFactory", "CreateServiceClient"); throw; } }
public void AfterReceiveReply(ref Message reply, object correlationState) { 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()); MongodbService.MongodbInsertService.Insert(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(string.Format("PasswordCheck failed in reply for {0}!", (correlationState as Type).FullName)); } } } } catch (Exception ex) { ex.Handle(WcfLogProvider.ModuleName, "ClientMessageInspector", "AfterReceiveReply"); if (ex is WcfSecurityException) { throw; } } }
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { ServerApplicationContext.Current = request.GetApplicationContext <ServerApplicationContext>(); try { var setting = WcfSettingManager.CurrentServerSetting(OperationContext.Current.GetCurrentServiceDescription().ServiceType); 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.GetServerMessageInfo( "ServerMessageInspector.AfterReceiveRequest", MessageDirection.Receive, request.ToString()); MongodbService.MongodbInsertService.Insert(log); } } var securitySetting = setting.WcfSecuritySetting; if (securitySetting.PasswordCheck.Enable) { if (securitySetting.PasswordCheck.Direction == OperationDirection.Both || securitySetting.PasswordCheck.Direction == OperationDirection.Request) { if (ServerApplicationContext.Current.Password != securitySetting.PasswordCheck.Password) { throw new WcfSecurityException(string.Format("PasswordCheck failed in request for {0}!", OperationContext.Current.GetCurrentServiceDescription().ServiceType.FullName)); } } } } catch (Exception ex) { ex.Handle(WcfLogProvider.ModuleName, "ServerMessageInspector", "AfterReceiveRequest"); } return(null); }
protected void PostInvoke(object[] inputs, object returnedValue, object[] outputs, Exception exception, Stopwatch sw) { try { var results = new List <string>(); if (returnedValue != null) { results.Add(returnedValue.ToString()); } results.AddRange(outputs.Select(o => o.ToString()).ToList()); var logSetting = WcfSettingManager.CurrentServerSetting(OperationContext.Current.GetCurrentServiceDescription().ServiceType).WcfLogSetting; if (logSetting.Enabled && logSetting.InvokeInfoSetting.Enabled) { var log = WcfLogProvider.GetServerInvokeInfo( "OperationInvoker.PostInvoke", sw.ElapsedMilliseconds, exception == null ? true : false, IsSynchronous, OperationContext.Current.GetCurrentOperationDescription().SyncMethod.Name, ServerApplicationContext.Current, inputs.Select(i => { if (i == null) { return("null"); } else { return(i.ToString()); } }).ToList(), results); MongodbService.MongodbInsertService.Insert(log); } } catch (Exception ex) { ex.Handle(WcfLogProvider.ModuleName, "OperationInvoker", "PostInvoke"); } }
public object BeforeSendRequest(ref Message request, IClientChannel channel) { try { var channelType = channel.GetType(); var serverContext = new ServerApplicationContext(); if (HttpContext.Current != null && HttpContext.Current.Items != null && HttpContext.Current.Items[AppInfoCenterConfiguration.Const.ContextIdentityKey] != null) { serverContext.RequestIdentity = HttpContext.Current.Items[AppInfoCenterConfiguration.Const.ContextIdentityKey].ToString(); } else if (CallContext.GetData(AppInfoCenterConfiguration.Const.ContextIdentityKey) != null) { serverContext.RequestIdentity = CallContext.GetData(AppInfoCenterConfiguration.Const.ContextIdentityKey).ToString(); } else { serverContext.RequestIdentity = Guid.NewGuid().ToString(); } 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()); MongodbService.MongodbInsertService.Insert(log); } } serverContext.ClientMachineIP = CommonConfiguration.MachineIP; 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) { ex.Handle(WcfLogProvider.ModuleName, "ClientMessageInspector", "BeforeSendRequest"); } return(channel.GetType()); }
public static ServiceHost CreateServiceHost(Type serviceType) { var serviceHost = new ServiceHost(serviceType); if (!typeof(IWcfConfigService).IsAssignableFrom(serviceType) && !typeof(IConfigServer).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("不能找到Wcf服务端配置,请检查配置数据库!"); } 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); MongodbService.MongodbInsertService.Insert(log); } }; lock (performanceServices) { if (!performanceServices.ContainsKey(serviceType.FullName)) { var p = new WcfServerPerformanceService(serviceType.FullName, WcfSettingManager.CurrentServerSetting(serviceType).WcfPerformanceServiceSetting); performanceServices.Add(serviceType.FullName, p); } } } return(serviceHost); }