| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using System;
- using System.ServiceModel;
- using System.ServiceProcess;
- using System.Threading;
- using Dongke.IBOSS.PRD.Basics.BaseResources;
- using Dongke.IBOSS.PRD.Basics.DataAccess;
- using Dongke.IBOSS.PRD.Basics.Library;
- using Dongke.IBOSS.PRD.WCF.Services;
- namespace Dongke.IBOSS.PRD.WCF.WindowsService
- {
- partial class Service1 : ServiceBase
- {
- private SynchronizationContext _syncContext;
- public Service1()
- {
- InitializeComponent();
- //_syncContext = SynchronizationContext.Current;
- // 启动内存回收(每小时)
- timer1.Interval = 1000 * 60 * 60;
- timer1.Start();
- }
- public void SetConfigData()
- {
- ConfigConst.GetConfigData(this.ServiceName);
- //this.ServiceName = string.Format("DK.IBOSS.PRD.[{0}].{1}", ConfigConst.ClientVersion, ConfigConst.WindowsServerName);
- // 更改连接串
- DataManager.ConnectionString = string.Format(DataManager.ConnectionStringFormat,
- ConfigConst.DBIP,
- ConfigConst.DBPort,
- ConfigConst.DBServerName,
- ConfigConst.DBUser,
- ConfigConst.DBPassword
- );
- }
- public ServiceHostCollection ServiceHosts
- {
- get;
- private set;
- }
- public void WCFStart()
- {
- try
- {
- if (this.ServiceHosts != null)
- {
- this.ServiceHosts.Dispose();
- this.ServiceHosts = null;
- }
- this.ServiceHosts = new ServiceHostCollection();
- this.ServiceHosts.IP = ConfigConst.WCFIP;
- this.ServiceHosts.Port = ConfigConst.WCFPort;
- this.ServiceHosts.Init();
- foreach (ServiceHost host in ServiceHosts)
- {
- host.Opening += host_Opening;
- host.Opened += host_Opened;
- host.Closing += host_Closing;
- host.Closed += host_Closed;
- }
- ServiceHostOpen();
- }
- catch (Exception ex)
- {
- LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
- throw ex;
- }
- }
- public void ServiceHostOpen()
- {
- try
- {
- foreach (ServiceHost host in ServiceHosts)
- {
- //
- //可以增加对控制
- //
- host.Open();
- }
- }
- catch (Exception ex)
- {
- LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
- throw ex;
- }
- }
- public void WCFStop()
- {
- try
- {
- if (this.ServiceHosts != null)
- {
- this.ServiceHosts.Dispose();
- this.ServiceHosts = null;
- }
- }
- catch (Exception ex)
- {
- LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
- throw ex;
- }
- }
- void host_Opening(object sender, EventArgs e)
- {
- LogFileOperation.Fatal(ConfigConst.LogFileName, "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开中......");
- //SendOrPostCallback callback = delegate(object state)
- //{
- // string strInfo =
- // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
- //};
- //_syncContext.Post(callback, null);
- }
- void host_Opened(object sender, EventArgs e)
- {
- LogFileOperation.Fatal(ConfigConst.LogFileName, "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开完毕。");
- //SendOrPostCallback callback = delegate(object state)
- //{
- // string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开完毕。";
- // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
- //};
- //_syncContext.Post(callback, null);
- }
- void host_Closed(object sender, EventArgs e)
- {
- LogFileOperation.Fatal(ConfigConst.LogFileName, "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭完毕。");
- //SendOrPostCallback callback = delegate(object state)
- //{
- // string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭完毕。";
- // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
- //};
- //_syncContext.Post(callback, null);
- }
- void host_Closing(object sender, EventArgs e)
- {
- LogFileOperation.Fatal(ConfigConst.LogFileName, "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭中......");
- //SendOrPostCallback callback = delegate(object state)
- //{
- // string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭中......";
- // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
- //};
- //_syncContext.Post(callback, null);
- }
- /// <summary>
- /// 服务开始
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void DKIMSSWCFService_ServiceDoingEvent(object sender, ServiceEventArgs e)
- {
- //SendOrPostCallback callback = delegate(object state)
- //{
- // string strInfo = "服务" + e.MethodName + " 服务开始。 ";
- // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
- //};
- //_syncContext.Post(callback, null);
- }
- /// <summary>
- /// 服务结束
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void DKIMSSWCFService_ServiceDoneEvent(object sender, ServiceEventArgs e)
- {
- //SendOrPostCallback callback = delegate(object state)
- //{
- // string strInfo = "服务" + e.MethodName + " 服务结束。 ";
- // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
- //};
- //_syncContext.Post(callback, null);
- }
- /// <summary>
- /// 服务异常
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void DKIMSSWCFService_ServiceExceptionEvent(object sender, ServiceEventArgs e)
- {
- //SendOrPostCallback callback = delegate(object state)
- //{
- // string strInfo = "服务" + e.MethodName + " 服务异常。 ";
- // LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
- //};
- //_syncContext.Post(callback, null);
- }
- protected override void OnStart(string[] args)
- {
- this.SetConfigData();
- this.WCFStart();
- }
- protected override void OnStop()
- {
- this.WCFStop();
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- try
- {
- GC.Collect();
- }
- catch
- {
- }
- }
- }
- }
|