/// <summary> /// 功能:创建对象实例 /// </summary> /// <typeparam name="T">实例的接口类型</typeparam> /// <param name="strAssemblyName">类型所在程序集名称</param> /// <param name="strClassNameSpace">类型所在命名空间</param> /// <param name="strClassName">类型名称</param> /// <param name="ignoreCase">类名是否区分大小写</param> /// <returns>对象实例</returns> public static T CreateTypeInstance <T>(string strAssemblyName, string strClassNameSpace, string strClassName, bool ignoreCase = false) { if (string.IsNullOrWhiteSpace(strAssemblyName) || string.IsNullOrWhiteSpace(strClassNameSpace) || string.IsNullOrWhiteSpace(strClassName)) { return(default(T)); } else { object objTemp = null; try { //命名空间.类型名 string fullName = strClassNameSpace + "." + strClassName; //加载程序集,创建程序集里面的 命名空间.类型名 实例 objTemp = Assembly.Load(strAssemblyName).CreateInstance(fullName, ignoreCase); //类型转换并返回 return((T)objTemp); } catch (Exception ex) { string strMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("ObjectFactoryHelper.CreateTypeInstance", strMessage, LogLevel.Error); return(default(T)); } } }
static void Main(string[] args) { try { XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("log4net.config")); //运行服务 HostFactory.Run(cf => { cf.SetServiceName("Lx.Service.LogHost"); cf.SetDisplayName("Lx.Service.LogHost"); cf.SetDescription("日志服务"); cf.Service <TopshelfWrapper>(sv => { sv.ConstructUsing(b => new TopshelfWrapper()); sv.WhenStarted(o => o.Start()); sv.WhenStopped(o => o.Stop()); }); cf.RunAsLocalSystem(); cf.StartAutomatically(); cf.EnablePauseAndContinue(); }); } catch (Exception ex) { string strMessage = $"服务启动失败,原因:{SysLogHelper.GetErrorLogInfo(ex, true)}"; SysLogHelper.LogMessage("LogHost.Main", strMessage); } Console.ReadLine(); }
/// <summary> /// 功能:创建对象实例 /// </summary> /// <typeparam name="T">实例的接口类型</typeparam> /// <param name="strClassFullName">实例的类全名称(命名空间.类型名,程序集)</param> /// <param name="ignoreCase">类全名是否区分大小写</param> /// <returns>对象实例</returns> public static T CreateTypeInstance <T>(string strClassFullName, bool ignoreCase = false) { if (!string.IsNullOrWhiteSpace(strClassFullName)) { object objTemp = null; try { //加载类型 Type typTemp = Type.GetType(strClassFullName, true, ignoreCase); //根据类型创建实例 objTemp = Activator.CreateInstance(typTemp, true); //objTemp = typTemp.Assembly.CreateInstance(strClassFullName, ignoreCase); return((T)objTemp); } catch (Exception ex) { string strMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("ObjectFactoryHelper.CreateTypeInstance", strMessage, LogLevel.Error); return(default(T)); } } else { return(default(T)); } }
/// <summary> /// 功能:静态构造函数,创建默认的缓存实例 /// </summary> static CacheHelper() { try { //获取缓存策略类别 string strCacheStrategyType = ConfigurationManager.AppSettings["CacheStrategyType"]; if (!string.IsNullOrWhiteSpace(strCacheStrategyType)) { //获取缓存过期时间 string strCacheExpiration = ConfigurationManager.AppSettings["CacheExpiration"]; double value; if (!string.IsNullOrWhiteSpace(strCacheExpiration)) { double.TryParse(strCacheExpiration, out value); s_cacheExpiration = TimeSpan.FromMinutes(value); } //创建缓存策略类实例 var redisConfigInfo = JsonConfigInfo.LoadFromFile("cache.json"); s_cacheStrategy = ComponentLoader.Load <ICacheStrategy>(redisConfigInfo); } } catch (Exception ex) { string strMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("CacheHelper.Static", strMessage, LogLevel.Error); } }
static void Main(string[] args) { try { XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("log4net.config")); //获取配置的服务名称列表 List <string> lstConfigServices = ServiceHelper.GetConfigServices(); //启动服务 if (null != lstConfigServices && lstConfigServices.Count > 0) { //获取Windows服务信息 JsonConfigInfo winServiceInfo = ConfigHelper.LoadFromFile("WinServiceInfo.json"); //获取待启动的Windows服务的名称 string strServiceInfo = lstConfigServices[0]; int intPosition = strServiceInfo.LastIndexOf("."); string strServiceName = strServiceInfo.Substring(intPosition + 1); //获取服务配置 JObject joServiceInfo = winServiceInfo.GetValue <JObject>(strServiceName); //运行服务 HostFactory.Run(cf => { cf.SetServiceName(joServiceInfo["ServiceName"].Value <string>()); cf.SetDisplayName(joServiceInfo["DisplayName"].Value <string>()); cf.SetDescription(joServiceInfo["Description"].Value <string>()); cf.Service <TopshelfWrapper>(sv => { sv.ConstructUsing(b => new TopshelfWrapper()); sv.WhenStarted(o => o.Start()); sv.WhenStopped(o => o.Stop()); }); cf.RunAsLocalSystem(); cf.StartAutomatically(); cf.EnablePauseAndContinue(); }); } else { Console.WriteLine("没有配置服务,无服务可启动!"); } } catch (Exception ex) { string strMessage = $"服务启动失败,原因:{SysLogHelper.GetErrorLogInfo(ex, true)}"; SysLogHelper.LogMessage("Lx.Service.WinServiceHost.Main", strMessage); } Console.ReadLine(); }
static ObjectFactoryHelper() { try { m_dictObject = new Dictionary <int, object>(); m_queryImplementAssembly = Assembly.Load(m_queryImplementAssemblyName); } catch (Exception ex) { string strMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("ObjectFactoryHelper.Static", strMessage, LogLevel.Error); } }
static LogServiceHelper() { try { JsonConfigInfo lsConfigInfo = ConfigHelper.LoadFromFile("LogService.json"); m_enableFileLog = lsConfigInfo.GetValue <bool>("EnableFileLog"); m_enableDataBaseLog = lsConfigInfo.GetValue <bool>("EnableDataBaseLog"); } catch (Exception ex) { string strMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("LogServiceHelper.static", strMessage); } }
/// <summary> /// 功能:初始化 /// </summary> public static void Initalize() { try { m_messageReceiver = MessageQueueHelper.GetMessageQueueFromPool().GetMessageReceiver(QueueName.LxLogQueue.ToString(), ""); m_messageReceiver.Received += m_messageReceiver_Received; m_messageReceiver.Start(); Console.WriteLine("日志消息队列消费者已启动!"); } catch (Exception ex) { string strErrorMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("LogMQConsumerHelper.Static", strErrorMessage); } }
/// <summary> /// 功能:初始化 /// </summary> public static void Initalize() { try { m_messageReceiver = MessageQueueHelper.GetMessageQueueFromPool().GetMessageReceiver(QueueName.AutoPayQueue.ToString(), ""); m_messageReceiver.Received += m_messageReceiver_Received; m_messageReceiver.Start(); Console.WriteLine("订单服务已启动!"); } catch (Exception ex) { string strMessage = $"初始化,原因:{SysLogHelper.GetErrorLogInfo(ex, true)}"; SysLogHelper.LogMessage("Lx.Service.Initalize", strMessage); } }
/// <summary> /// 功能:停止服务 /// </summary> public void Stop() { try { //关闭服务 OrderMQConsumerHelper.Release(); //调用垃圾回收 GC.Collect(); GC.WaitForPendingFinalizers(); } catch (Exception ex) { string strMessage = $"服务停止失败,原因:{SysLogHelper.GetErrorLogInfo(ex, true)}"; SysLogHelper.LogMessage("Lx.Service.Order", strMessage); } }
/// <summary> /// 接收代付订单 /// </summary> /// <param name="sender">事件源</param> /// <param name="e">消息参数</param> private static void m_messageReceiver_Received(object sender, MessageEventArgs e) { var orderModel = e.Message as OrderMqMessage; try { if (null != orderModel) { PayOrderService.AddPayOrder(orderModel); } } catch (Exception ex) { string strMessage = $"接收代付订单失败,原因:{SysLogHelper.GetErrorLogInfo(ex, true)}"; SysLogHelper.LogMessage("Lx.Service.m_messageReceiver_Received", strMessage); } }
/// <summary> /// 功能:启动服务 /// </summary> public void Start() { try { //获取配置的服务名称列表 List <string> lstConfigServices = ServiceHelper.GetConfigServices(); //启动服务主机 lstConfigServices.ForEach(cs => { //获取待启动的Windows服务的名称 int intPosition = cs.LastIndexOf("."); string strServiceName = cs.Substring(intPosition + 1); //日志服务 if (cs.IndexOf("LogService", StringComparison.InvariantCultureIgnoreCase) > 0) { m_logServiceHost = new ServiceHost <LogService>(); m_logServiceHost.EnableMetadataExchange = true; m_logServiceHost.Open(); Console.WriteLine("日志服务已经启动,如果要退出,请输入exit,不要直接关闭"); } //用户服务 if (cs.IndexOf("UserService", StringComparison.InvariantCultureIgnoreCase) > 0) { m_userServiceHost = new ServiceHost <UserService>(); m_userServiceHost.EnableMetadataExchange = true; m_userServiceHost.Open(); Console.WriteLine("用户服务已经启动,如果要退出,请输入exit,不要直接关闭"); } //订单服务 if (cs.IndexOf("OrderService", StringComparison.InvariantCultureIgnoreCase) > 0) { m_OrderServiceHost = new ServiceHost <OrderService>(); m_OrderServiceHost.EnableMetadataExchange = true; m_OrderServiceHost.Open(); Console.WriteLine("订单服务已经启动,如果要退出,请输入exit,不要直接关闭"); } }); } catch (Exception ex) { string strMessage = $"服务启动失败,原因:{SysLogHelper.GetErrorLogInfo(ex, true)}"; SysLogHelper.LogMessage("Lx.Service.WinServiceHost.Start", strMessage); } }
/// <summary> /// 功能:创建对象实例 /// </summary> /// <typeparam name="T">实例的接口类型</typeparam> /// <param name="strClassName">实例的类名称</param> /// <param name="strClassNameSpace">实例的类所在的空间名称</param> /// <param name="blnCreateNew">是否每次创建新对象</param> /// <returns>对象实例</returns> public static T CreateInstance <T>(string strClassName, string strClassNameSpace, bool blnCreateNew = false) { if (!string.IsNullOrWhiteSpace(strClassName)) { object objTemp = null; string strInstanceName = ""; if (string.IsNullOrWhiteSpace(strClassNameSpace)) { strInstanceName = m_queryImplementAssemblyName + "." + strClassName; } else { strInstanceName = m_queryImplementAssemblyName + "." + strClassNameSpace + "." + strClassName; } try { if (blnCreateNew) { objTemp = m_queryImplementAssembly.CreateInstance(strInstanceName); } else { int intKey = strInstanceName.GetHashCode(); if (!m_dictObject.TryGetValue(intKey, out objTemp)) { objTemp = m_queryImplementAssembly.CreateInstance(strInstanceName); m_dictObject[intKey] = objTemp; } } } catch (Exception ex) { string strMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("ObjectFactoryHelper.CreateInstance", strMessage, LogLevel.Error); return(default(T)); } return((T)objTemp); } else { return(default(T)); } }
static DBDataSourceHelper() { try { //获取数据源类型 string strDBType = ConfigurationManager.AppSettings["DBType"]; if (string.IsNullOrWhiteSpace(strDBType)) { strDBType = "MySql"; } s_dbDataSource = ObjectFactoryHelper.CreateInstance <IDBDataSource>(strDBType + "DBDataSource", "DBDataSource", true); } catch (Exception ex) { string strMessage = SysLogHelper.GetErrorLogInfo(ex, true); SysLogHelper.LogMessage("DataSourceHelper.Static", strMessage, LogLevel.Error); } }
/// <summary> /// 功能:停止服务 /// </summary> public void Stop() { try { //关闭服务 if (null != m_logServiceHost) { if (m_logServiceHost.State != CommunicationState.Closed) { m_logServiceHost.Close(); } m_logServiceHost = null; } if (null != m_userServiceHost) { if (m_userServiceHost.State != CommunicationState.Closed) { m_userServiceHost.Close(); } m_userServiceHost = null; } if (null != m_OrderServiceHost) { if (m_OrderServiceHost.State != CommunicationState.Closed) { m_OrderServiceHost.Close(); } m_OrderServiceHost = null; } //调用垃圾回收 GC.Collect(); GC.WaitForPendingFinalizers(); } catch (Exception ex) { string strMessage = $"服务启动失败,原因:{SysLogHelper.GetErrorLogInfo(ex, true)}"; SysLogHelper.LogMessage("Lx.Service.WinServiceHost.Stop", strMessage); } }