Service1.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.ServiceModel;
  3. using System.ServiceProcess;
  4. using System.Threading;
  5. using Dongke.IBOSS.PRD.Basics.BaseResources;
  6. using Dongke.IBOSS.PRD.Basics.DataAccess;
  7. using Dongke.IBOSS.PRD.Basics.Library;
  8. using Dongke.IBOSS.PRD.WCF.Services;
  9. namespace Dongke.IBOSS.PRD.WCF.WindowsService
  10. {
  11. partial class Service1 : ServiceBase
  12. {
  13. private SynchronizationContext _syncContext;
  14. public Service1()
  15. {
  16. InitializeComponent();
  17. //_syncContext = SynchronizationContext.Current;
  18. // 启动内存回收(每小时)
  19. timer1.Interval = 1000 * 60 * 60;
  20. timer1.Start();
  21. }
  22. public void SetConfigData()
  23. {
  24. ConfigConst.GetConfigData(this.ServiceName);
  25. //this.ServiceName = string.Format("DK.IBOSS.PRD.[{0}].{1}", ConfigConst.ClientVersion, ConfigConst.WindowsServerName);
  26. // 更改连接串
  27. DataManager.ConnectionString = string.Format(DataManager.ConnectionStringFormat,
  28. ConfigConst.DBIP,
  29. ConfigConst.DBPort,
  30. ConfigConst.DBServerName,
  31. ConfigConst.DBUser,
  32. ConfigConst.DBPassword
  33. );
  34. }
  35. public ServiceHostCollection ServiceHosts
  36. {
  37. get;
  38. private set;
  39. }
  40. public void WCFStart()
  41. {
  42. try
  43. {
  44. if (this.ServiceHosts != null)
  45. {
  46. this.ServiceHosts.Dispose();
  47. this.ServiceHosts = null;
  48. }
  49. this.ServiceHosts = new ServiceHostCollection();
  50. this.ServiceHosts.IP = ConfigConst.WCFIP;
  51. this.ServiceHosts.Port = ConfigConst.WCFPort;
  52. this.ServiceHosts.Init();
  53. foreach (ServiceHost host in ServiceHosts)
  54. {
  55. host.Opening += host_Opening;
  56. host.Opened += host_Opened;
  57. host.Closing += host_Closing;
  58. host.Closed += host_Closed;
  59. }
  60. ServiceHostOpen();
  61. }
  62. catch (Exception ex)
  63. {
  64. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  65. throw ex;
  66. }
  67. }
  68. public void ServiceHostOpen()
  69. {
  70. try
  71. {
  72. foreach (ServiceHost host in ServiceHosts)
  73. {
  74. //
  75. //可以增加对控制
  76. //
  77. host.Open();
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  83. throw ex;
  84. }
  85. }
  86. public void WCFStop()
  87. {
  88. try
  89. {
  90. if (this.ServiceHosts != null)
  91. {
  92. this.ServiceHosts.Dispose();
  93. this.ServiceHosts = null;
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  99. throw ex;
  100. }
  101. }
  102. void host_Opening(object sender, EventArgs e)
  103. {
  104. LogFileOperation.Fatal(ConfigConst.LogFileName, "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开中......");
  105. //SendOrPostCallback callback = delegate(object state)
  106. //{
  107. // string strInfo =
  108. // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  109. //};
  110. //_syncContext.Post(callback, null);
  111. }
  112. void host_Opened(object sender, EventArgs e)
  113. {
  114. LogFileOperation.Fatal(ConfigConst.LogFileName, "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开完毕。");
  115. //SendOrPostCallback callback = delegate(object state)
  116. //{
  117. // string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开完毕。";
  118. // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  119. //};
  120. //_syncContext.Post(callback, null);
  121. }
  122. void host_Closed(object sender, EventArgs e)
  123. {
  124. LogFileOperation.Fatal(ConfigConst.LogFileName, "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭完毕。");
  125. //SendOrPostCallback callback = delegate(object state)
  126. //{
  127. // string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭完毕。";
  128. // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  129. //};
  130. //_syncContext.Post(callback, null);
  131. }
  132. void host_Closing(object sender, EventArgs e)
  133. {
  134. LogFileOperation.Fatal(ConfigConst.LogFileName, "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭中......");
  135. //SendOrPostCallback callback = delegate(object state)
  136. //{
  137. // string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭中......";
  138. // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  139. //};
  140. //_syncContext.Post(callback, null);
  141. }
  142. /// <summary>
  143. /// 服务开始
  144. /// </summary>
  145. /// <param name="sender"></param>
  146. /// <param name="e"></param>
  147. void DKIMSSWCFService_ServiceDoingEvent(object sender, ServiceEventArgs e)
  148. {
  149. //SendOrPostCallback callback = delegate(object state)
  150. //{
  151. // string strInfo = "服务" + e.MethodName + " 服务开始。 ";
  152. // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  153. //};
  154. //_syncContext.Post(callback, null);
  155. }
  156. /// <summary>
  157. /// 服务结束
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. void DKIMSSWCFService_ServiceDoneEvent(object sender, ServiceEventArgs e)
  162. {
  163. //SendOrPostCallback callback = delegate(object state)
  164. //{
  165. // string strInfo = "服务" + e.MethodName + " 服务结束。 ";
  166. // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  167. //};
  168. //_syncContext.Post(callback, null);
  169. }
  170. /// <summary>
  171. /// 服务异常
  172. /// </summary>
  173. /// <param name="sender"></param>
  174. /// <param name="e"></param>
  175. void DKIMSSWCFService_ServiceExceptionEvent(object sender, ServiceEventArgs e)
  176. {
  177. //SendOrPostCallback callback = delegate(object state)
  178. //{
  179. // string strInfo = "服务" + e.MethodName + " 服务异常。 ";
  180. // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  181. //};
  182. //_syncContext.Post(callback, null);
  183. }
  184. protected override void OnStart(string[] args)
  185. {
  186. this.SetConfigData();
  187. this.WCFStart();
  188. }
  189. protected override void OnStop()
  190. {
  191. this.WCFStop();
  192. }
  193. private void timer1_Tick(object sender, EventArgs e)
  194. {
  195. try
  196. {
  197. GC.Collect();
  198. }
  199. catch
  200. {
  201. }
  202. }
  203. }
  204. }