一、建立Windows Service
新建工程选择windows service,会自动生成Service1,这里新建了一个FirstService,在windows service中经常用timers来控制service的运行。以下是代码
Code
partial class FirstService : ServiceBase
{
System.Timers.Timer timer = new System.Timers.Timer();
string path = ConfigurationManager.AppSettings["path"];
public FirstService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 1000 * 5;
timer.Enabled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Enabled = false;
try
{
SaveLog("hello world");
}
catch (Exception ex)
{
SaveLog(ex.Message);
}
finally
{
timer.Interval = 1000 * 5;
timer.Enabled = true;
}
}
protected override void OnStop()
{
timer.Enabled = false;
}
private void SaveLog(string input)
{
string logPath = path + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
FileStream fs = new FileStream(logPath, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.Write("Began Entry:");
sw.Write("\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + input);
sw.WriteLine();
sw.Flush();
sw.Close();
}
}
这个service要实现的就是每隔5秒钟向一个txt文件中写入"hello world"
partial class FirstService : ServiceBase
{
System.Timers.Timer timer = new System.Timers.Timer();
string path = ConfigurationManager.AppSettings["path"];
public FirstService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 1000 * 5;
timer.Enabled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Enabled = false;
try
{
SaveLog("hello world");
}
catch (Exception ex)
{
SaveLog(ex.Message);
}
finally
{
timer.Interval = 1000 * 5;
timer.Enabled = true;
}
}
protected override void OnStop()
{
timer.Enabled = false;
}
private void SaveLog(string input)
{
string logPath = path + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
FileStream fs = new FileStream(logPath, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.Write("Began Entry:");
sw.Write("\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + input);
sw.WriteLine();
sw.Flush();
sw.Close();
}
}
完成之后在该service的设计器上右键单击,选择"Add Installer",会生成对应的project installer,注意它包含了两个installer
serviceProcessInstaller1的Account属性为该service在哪种帐户类型下运行,local stystem为本地系统,user为登入用户
serviceInstaller1的ServiceName属性为对应的service名字,即FirstService
二、安装、启动Windows Service
建置工程,通过以后,就可安装了。安装的常用命令如下:
1.安装Service:InstallUtil FirstService.exe
2.卸载Service:InstallUtil /u FirstService.exe
3.启动Service:Net start FirstService
4.停止Service:Net stop FirstService
注意:这些命令均在.net framework的命令提示符下运行,启动与停止service也可以在服务列表中手动实现。
三、调试Windows Service
windows service不能直接调试,参照了一下众网友的做法,主要有一下几种:
1.写日志。即在自己觉得会出错的地方把信息写入日志,类似以上实现的service功能。但是无法单步调试,不方便
2.附加进程。这样可以象正常的windows application一样进行单步调试,但是,必须在启动服务之后,才能附加进程。附加进程时选择显示该用户的所有进程即可显示service的进程,如图所示
如果需要在启动时就进行调试,则需要在OnStart启动方法中添加延时处理,但是不能超过30秒,因为服务的最大启动时间为30秒,超过会报错。
3.注释掉原Main方法中的代码,自己手动添加启动代码。
注释掉以下代码
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]{ new FirstService()};
ServiceBase.Run(ServicesToRun);
改为
ServicesToRun = new ServiceBase[]{ new FirstService()};
ServiceBase.Run(ServicesToRun);
FirstService firstService
=
new
FirstService();
firstService.OnStart();
注意把protected override void OnStart(string[] args)改为public void OnStart()
firstService.OnStart();
此方法需要停止服务。