如何将VS下Windows服务项目生成的exe注册为windows服务,让其直接从后台运行
方法一:使用windows自带的命令sc,首先我们要用管理员运行cmd
注册服务:
sc create TestService binpath= "D:\Test\Test.WindowsService.exe" start= auto displayname= "Windows测试服务"
注意:所有的等号和值之间需要一个空格(等号前不要空格,等号后要一个空格) !!
——binpath和等号之间不要空格,等号和安装路径(例如"= D:\Test\Test.WindowsService.exe")之间要空格
binpath:你的应用程序所在的路径。
displayname:服务显示的名称。
description:服务描述说明。
如何判断服务是否注册成功:
在cmd中输入services.msc打开系统服务,查看是否出现“Windows测试服务”(即displayname=后面的参数,我这里设置的是“Windows测试服务”)
修改描述:
sc description TestService "这是一个Windows测试服务。"
启动服务:
net start TestService
停止服务:
net stop TestService
删除服务:
sc delete TestService
SC命令的更多用法请在查看help sc
使用时我们一般用个文本文档写入命令然后保存成bat格式的文件,添加服务一个,删除服务一个。
例如 “添加服务.bat” 或 Install.bat 内容如下:
@echo off
echo 正在安装服务请稍等...
set curdir=%~dp0
cd /d %curdir%
sc create TestService binpath= "D:\Test\Test.WindowsService.exe" start= auto displayname= "Windows测试服务"
sc description TestService "Windows测试服务,不要删除"
echo -----------------------------
echo 服务安装成功
echo -----------------------------
echo 正在启动服务请稍等...
net start TestService
echo -----------------------------
echo 服务启动成功
echo -----------------------------
pause
“删除服务.bat” 或 unInstall.bat 内容如下:
@echo off
echo 正在停止服务请稍等...
net stop TestService
echo -----------------------------
echo 服务停止成功
echo -----------------------------
echo 正在卸载服务请稍等...
set curdir=%~dp0
cd /d %curdir%
sc delete TestService
echo -----------------------------
echo 服务卸载成功
echo -----------------------------
pause
方法二:在Windows服务项目中引入(Topshelf是一个开源的跨平台的宿主服务框架),并且在Program.cs 和 App.config 中做如下配置:
Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
using Test.WindowsService.BusService;
namespace Test.WindowsService
{
///
/// Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono
///
public static class Program
{
///
/// 应用程序的主入口点。
///
public static void Main()
{
//ServiceBase[] ServicesToRun;
//ServicesToRun = new ServiceBase[]
//{
// new Service1()
//};
//ServiceBase.Run(ServicesToRun);
try
{
var serviceName = System.Configuration.ConfigurationManager.AppSettings["ServiceName"];
var displayName = System.Configuration.ConfigurationManager.AppSettings["DisplayName"];
var description = System.Configuration.ConfigurationManager.AppSettings["Description"];
//方式一加载配置文件(配置文件和exe执行文件放在同一目录,配置文件属性设置成始终复制)
//string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
//string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath);
//string configFilePath = assemblyDirPath + "\\log4net.config";
//log4net.Config.XmlConfigurator.Configure(new FileInfo(configFilePath));
//log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
//配置和运行宿主服务
HostFactory.Run(x =>
{
//方式二加载配置文件(配置文件和exe执行文件放在同一目录,配置文件属性设置成始终复制)
x.UseLog4Net("log4net.config");
//x.UseLog4Net();
//注册服务
//x.Service(settings => new TopshelfService());
//x.Service
x.Service
//x.Service
//{
// s.ConstructUsing(name => new TimerService());
// s.WhenStarted(tc => tc.Start());
// s.WhenStopped(tc => tc.Stop());
// s.WhenPaused(tc => tc.Stop());
// s.WhenContinued(tc => tc.Start());
//});
//x.Service
//{
// //指定服务类型
// s.ConstructUsing(name => new DemoService());
// //当服务启动后执行什么
// s.WhenStarted(tc => tc.Start());
// //当服务停止后执行什么
// s.WhenStopped(tc => tc.Stop());
//});
//服务用本地系统账号来运行
x.RunAsLocalSystem();
//服务描述信息
x.SetDescription(description);
//服务显示名称
x.SetDisplayName(displayName);
//服务名称
x.SetServiceName(serviceName);
////服务描述信息
//x.SetDescription("Windows测试服务,不要删除");
////服务显示名称
//x.SetDisplayName("Windows测试服务");
////服务名称
//x.SetServiceName("TestService");
x.EnablePauseAndContinue();
});
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
View Code
App.config
View Code
log4net.config
View Code
例如 “添加服务.bat” 或 Install.bat 内容如下:
@echo off
echo 正在安装服务请稍等...
set curdir=%~dp0
cd /d %curdir%
..\bin\Debug\Test.WindowsService.exe install
echo -----------------------------
echo 服务安装成功
echo -----------------------------
echo 正在启动服务请稍等...
net start TestService
echo -----------------------------
echo 服务启动成功
echo -----------------------------
pause
“删除服务.bat” 或 unInstall.bat 内容如下:
@echo off
echo 正在停止服务请稍等...
net stop TestService
echo -----------------------------
echo 服务停止成功
echo -----------------------------
echo 正在卸载服务请稍等...
set curdir=%~dp0
cd /d %curdir%
..\bin\Debug\Test.WindowsService.exe uninstall
echo -----------------------------
echo 服务卸载成功
echo -----------------------------
pause
快捷方式:以管理员身份运行bat文件安装和卸载
下面是用手动方式服务安装、启动和卸载
使用管理员身份cmd命令进入相应的文件夹位置
安装:Test.WindowsService.exe install
启动:Test.WindowsService.exe start
停止:Test.WindowsService.exe stop
卸载:Test.WindowsService.exe uninstall
方法三:
无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer、Windows服务器管理工具或NET START命令启动它
1. 以管理员身份运行cmd
2. 安装windows服务
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319(InstallUtil.exe的路径,注意InstallUtil.exe的版本号需要和项目的版本号相同)
3. 安装windows服务
InstallUtil.exe D:\Service\需要安装的服务.exe(项目的路径)
4. 启动windows服务
net start TestServive(服务名称)