一、创建一个Windows Service
1)创建Windows Service项目
2)对Service重命名
将Service1重命名为你服务名称,这里我们命名为ServiceTest。
二、创建服务安装程序
1)添加安装程序
之后我们可以看到上图,自动为我们创建了ProjectInstaller.cs以及2个安装的组件。 也可以用代码创建这些
2)修改安装服务名
右键serviceInsraller1,选择属性,将ServiceName的值改为ServiceTest。
3)修改安装权限
右键serviceProcessInsraller1,选择属性,将Account的值改为LocalSystem。
三、写入服务代码
1)打开ServiceTest代码
右键ServiceTest,选择查看代码。
2)写入Service逻辑
添加如下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace WindowsServiceTest
{
public partial class ServiceTest : ServiceBase
{
public ServiceTest()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");
}
}
protected override void OnStop()
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");
}
}
}
}
这里我们的逻辑很简单,启动服务的时候写个日志,关闭的时候再写个日志。
四:安装和卸载windows服务
其实不用那么麻烦,
安装:步骤:win+R -----CMD -------输入microsoft的framework的相应版本的执行程序位置(如:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe 空一格再输入你的服务的执行文件的路径(如:D:\zt_documents\服务\SSY.DealerLoginStateService.root\SSY.DealerLoginStateService\SSY.LoginStateService\SSY.LoginStateService\bin\Debug\XX.exe(这里的.exe写自己的项目的名称)
卸载:步骤:win+R -----CMD -------输入microsoft的framework的相应版本的执行程序位置(如:C:\Windows\Microsoft.net\Framework\v4.0.30319\InstallUtil.exe -u 空一格再输入你的服务的执行文件的路径(如:D:\zt_documents\服务\SSY.DealerLoginStateService.root\SSY.DealerLoginStateService\SSY.LoginStateService\SSY.LoginStateService\bin\Debug\XX.exe(这里的.exe写自己的项目的名称)
如果出现安装有问题,提示权限问题,或者InstallUtil.exe不是有些的win32程序,直接把可以安装目录下面的复杂过去就可以安装了
五:服务定时任务小例子
- partial class GetUnitePService : ServiceBase
- {
- public GetUnitePService()
- {
- InitializeComponent();
- }
- Timer t;
- protected override void OnStart(string[] args)
- {
- t = new Timer();
- t.Interval = 5*60 * 1000;
- t.Elapsed += t_Elapsed;
- t.Start();
- WriteToLog("开始服务");
- }
- void t_Elapsed(object sender, ElapsedEventArgs e)
- {
- WriteToLog("开始执行一次获取," + DateTime.Now);
- }
- protected override void OnStop()
- {
- t.Stop();
- WriteToLog("停止服务");
- }
- private void getUnitePrice()
- {
- GetPriceSvc.GetPriceSvcClient gpc = new GetPriceSvc.GetPriceSvcClient();
- GetPriceSvc.ReturnDataOfArrayOfPriceInfo_DTODhuO2YUv res = gpc.GetPriceInfoByCompany("3ma5c094-7d4c-4f8c-8e8f-3530f18ca8kc", "fygjGetUnitePrice");
- WriteToLog("获取完了得到的条数:" + res.Value.Count() + " ," + DateTime.Now);
- }
- private void WriteToLog(string str)
- {
- try
- {
- string strdt = DateTime.Now.ToString("yyyy-MM-dd");
- FileStream aFile = new FileStream("D:\\LogGetUnitePrice\\" + strdt + ".txt", FileMode.Append);
- StreamWriter sw = new StreamWriter(aFile, Encoding.Default);
- sw.WriteLine(str);
- sw.Close();
- }
- catch { }
- }
- }
六:后台设置服务名称,可以写在配置文件中读取
或者直接在安装的时候从命名即可
- [RunInstaller(true)]
- public partial class ProjectInstaller : System.Configuration.Install.Installer
- {
- private System.ServiceProcess.ServiceInstaller serviceInstaller1;
- private ServiceProcessInstaller processInstaller;
- public ProjectInstaller()
- {
- InitializeComponent();
- processInstaller = new ServiceProcessInstaller();
- serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
- processInstaller.Account = ServiceAccount.LocalService;
- serviceInstaller1.StartType = ServiceStartMode.Automatic;//自动启动
- serviceInstaller1.ServiceName = "xkAutoAdvert";
- serviceInstaller1.Description = "讯酷网广告统计消息队列处理服务";
- Installers.Add(serviceInstaller1);
- Installers.Add(processInstaller);
- }
- }