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);
}
///
/// 服务开始
///
///
///
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);
}
///
/// 服务结束
///
///
///
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);
}
///
/// 服务异常
///
///
///
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
{
}
}
}
}