使用VS创建windows服务项目:

C#创建Windows服务(附服务安装)_启动方式

创建好项目 会出现一个设计界面 右键弹出对话框 选择添加安装程序

C#创建Windows服务(附服务安装)_sed_02

名字什么的自己可以改:

C#创建Windows服务(附服务安装)_.net_03

项目目录:

C#创建Windows服务(附服务安装)_启动方式_04

打开项目中的ProjectInstaller.Designer.cs 修改windows服务名称描述以及启动方式等:

点击查看代码

partial class ProjectInstaller
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 组件设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();

// 创建ServiceProcessInstaller对象和ServiceInstaller对象
this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();


// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Username = null;
this.spInstaller.Password = null;

// 设定服务名称
this.sInstaller.ServiceName = "CallApiExeTask";

//服务描述
this.sInstaller.Description = "定时调用api接口,获取任务后操作数据库";

// 设定服务的启动方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.spInstaller,
this.sInstaller});

}

#endregion

private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
}


打开Service1 写入想要执行的操作等:

点击查看代码

public partial class Service1 : ServiceBase
{
System.Timers.Timer timer1; //计时器
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
//服务开启执行代码
//定时调用接口

timer1 = new System.Timers.Timer();
timer1.Interval = 3000; //设置计时器事件间隔执行时间
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.Enabled = true;



}
/// <summary>
/// 定时器 调用的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://192.168.10.239:9000/");//接口url
string data = client.GetStringAsync("ZyTest").Result;//接口action
}

protected override void OnStop()
{
//服务结束执行代码
this.timer1.Enabled = false;
}


protected override void OnPause()
{
//服务暂停执行代码
base.OnPause();
}
protected override void OnContinue()
{
//服务恢复执行代码
base.OnContinue();
}
protected override void OnShutdown()
{
//系统即将关闭执行代码
base.OnShutdown();
}
}


Program.cs中可以设置主方法调用的service服务:

点击查看代码

static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}


生成解决方案,以上就完成了windows服务的编写

下面需要把服务安装到服务器或者pc上:

首先在网上下载一个installutil.exe文件(百度直接搜索可以下载) 放到...\bin\Debug文件夹下:

C#创建Windows服务(附服务安装)_windows服务_05

用管理员身份打开命令提示符:

C#创建Windows服务(附服务安装)_windows服务_06

输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回车

如果你的程序是2.0的framework则需要输入 cd C:\Windows\Microsoft.NET\Framework\v2.0.50727

C#创建Windows服务(附服务安装)_ide_07

输入 InstallUtil.exe D:\work\windows服务\WinService4ExeApi\WinService4ExeApi\bin\Debug\WinService4ExeApi.exe 回车 完成安装

说明: D:\work\windows服务\WinService4ExeApi\WinService4ExeApi\bin\Debug\WinService4ExeApi.exe表示项目生成的exe文件位置(也可以把debug文件夹单独copy到其他地方重命名)

C#创建Windows服务(附服务安装)_.net_08

安装完成后:打开服务就可以看到了

C#创建Windows服务(附服务安装)_sed_09

如果需要卸载此服务:打开cmd 直接输入 sc delete CallApiExeTask便可  CallApiExeTask为你的服务名称

C#创建Windows服务(附服务安装)_启动方式_10

ps: 如果你的windows服务程序修改了 需要更新成新版本 不用卸载服务再安装 只需先停掉该服务 然后把文件夹内的文件替换为新的文件 重新启动该服务即可