1.windows services管理
我们可以使用services.msc运行服务MMC(Microsoft Management Console),查看当前的windows服务和对服务进行操作!
也可以使用以下sc命令,sc的用法如下:
1. 安装服务
sc create 服务名称 binpath= "服务执行的命令" displayname= "显示名称" depend= Tcpip start= auto
其中网络连接使用TCP/IP,自动启动
2. 删除服务
sc delete 服务名称
删除后可能需要重启动才行
3. 修改配置
sc config 服务名称 binpath= "新命令" displayname= "新显示名称" depend= Tcpip
4. 设置为自启动
sc config 服务名称 start= auto
2.部署服务
将Remoting部署为windows服务,需要创建一个windows服务项目,并为该服务添加一个安装程序
设置好ServiceName,Account,StartType等属性
windows service继承自ServiceBase类,我们可以通过重写ServiceBase的OnStart方法并在该方法中添加自己的代码
public partial class RemoteSample : ServiceBase
{
public RemoteSample()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//our code in here
}
}
创建一个windows服务,在这里我们通过读取配置文件来创建Remoting
//FILE:app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="HuiShi.FoodOrdering.Service">
<service>
<wellknown type="HuiShi.FoodOrdering.DAL.FoodHelper,HuiShi.FoodOrdering.DAL" objectUri="food.service" mode="Singleton"/>
</service>
<channels>
<channel ref="tcp" port="10015"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
//Service1.cs
public partial class Service : ServiceBase
{
public Service()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
RemotingConfiguration.Configure(AppDomain.CurrentDomain.BaseDirectory + "HuiShi.FoodOrdering.Service.exe.config", true);
}
protected override void OnStop()
{
}
}
注:RemotingConfiguration.Configure(filepath,bool security); filepath需加上AppDomain.CurrentDomain.BaseDirectory,否则安装程序将找不到配置文件
3.安装服务:
用vs命令行工具运行 installutil filepath,installutil工具位于.net framework版本安装目录下
4.启动服务:sc start service_name
5.安装完成后自动启动服务功能:
注册Installer的AfterInstall事件,在AfterInstall中创建CMD进程,使用sc start server_name启动服务
示例代码如下:
using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
namespace RemoteSample.WindowsService
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
this.AfterInstall += ProjectInstaller_AfterInstall;
}
void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.CreateNoWindow = true; //在不显示窗口的情况下运行进程
p.StartInfo.UseShellExecute = false; //是否启动系统外壳
p.StartInfo.RedirectStandardInput = true; //是否从StandardInput中读取输入
p.StartInfo.RedirectStandardOutput = true; //是否从StandardInput中读取输出
p.Start();
p.StandardInput.WriteLine("sc start "+new RemoteSample().ServiceName);
p.StandardInput.WriteLine("exit"); //退出控制台
p.Dispose();
}
}
}
我们需要获取windows服务安装文件的地址(servicePath),并且我们必须知道windows服务的名称(serviceName)
添加以下代码实现选取windows服务安装文件安装并启动
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.CreateNoWindow = true; //在不显示窗口的情况下运行进程
p.StartInfo.UseShellExecute = false; //是否启动系统外壳
p.StartInfo.RedirectStandardInput = true; //是否从StandardInput中读取输入
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.StandardInput.WriteLine(@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe " +
servicePath);
p.StandardInput.WriteLine("sc start "+serviceName);
p.StandardInput.WriteLine("exit");
7.使用批处理安装和卸载服务
安装install.bat
echo 开始安装游戏服务 ::使用参数%1 sc create CnChessgds binpath= "%1/CnChess/Jianghuyou.GameService.exe" start= auto obj= LocalSystem DisplayName= "中国象棋游戏服务" ::不使用参数 sc create CnChessgds binpath= "C:/Program files/ops/game/CnChess/Jianghuyou.GameService.exe" start= auto obj= LocalSystem DisplayName= "中国象棋游戏服务" echo 安装完成!任意键盘退出 pause
卸载uninstall.bat
echo 开始删除游戏服务 sc stop CnChessgds sc delete CnChessgds echo 已经卸载!任意键盘退出 pause