asp.net remoting部署windows服务篇
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
东方网新,为您提供专业的网站建设,软件开发,网络营销SEO,网络推广服务,代理国外主机空间,域名注册,服务热线:(+86)18608275575,电子邮件:master#atnet.cc;欢迎您访问东方网新网站:www.atnet.cc
2.部署服务
将Remoting部署为windows服务,需要创建一个windows服务项目,并为该服务添加一个安装程序
设置好ServiceName,Account,StartType等属性
windows service继承自ServiceBase类,我们可以通过重写ServiceBase的OnStart方法并在该方法中添加自己的代码
01 |
public partial class RemoteSample : ServiceBase |
05 |
InitializeComponent(); |
07 |
protected override void OnStart(string[] args) |
3.安装服务:
用vs命令行工具运行 installutil filepath,installutil工具位于.net framework版本安装目录下
4.启动服务:sc start service_name
5.安装完成后自动启动服务功能:
注册Installer的AfterInstall事件,在AfterInstall中创建CMD进程,使用sc start server_name启动服务
东方网新,为您提供专业的网站建设,软件开发,网络营销SEO,网络推广服务,代理国外主机空间,域名注册,服务热线:(+86)18608275575,电子邮件:master#atnet.cc;欢迎您访问东方网新网站:www.atnet.cc
示例代码如下:
02 |
using System.ComponentModel; |
03 |
using System.Configuration.Install; |
04 |
using System.Diagnostics; |
06 |
namespace RemoteSample.WindowsService |
09 |
public partial class ProjectInstaller : Installer |
11 |
public ProjectInstaller() |
13 |
InitializeComponent(); |
14 |
this.AfterInstall += ProjectInstaller_AfterInstall; |
17 |
void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) |
19 |
Process p = new Process(); |
20 |
p.StartInfo.FileName = "cmd.exe"; |
21 |
p.StartInfo.CreateNoWindow = true; |
22 |
p.StartInfo.UseShellExecute = false; |
23 |
p.StartInfo.RedirectStandardInput = true; |
24 |
p.StartInfo.RedirectStandardOutput = true; |
26 |
p.StandardInput.WriteLine("sc start "+new RemoteSample().ServiceName); |
27 |
p.StandardInput.WriteLine("exit"); |
6.创建一个安装windows服务并启动的应用
我们需要获取windows服务安装文件的地址(servicePath),并且我们必须知道windows服务的名称(serviceName)
添加以下代码实现选取windows服务安装文件安装并启动
01 |
Process p = new Process(); |
02 |
p.StartInfo.FileName = "cmd.exe"; |
03 |
p.StartInfo.CreateNoWindow = true; |
04 |
p.StartInfo.UseShellExecute = false; |
05 |
p.StartInfo.RedirectStandardInput = true; |
06 |
p.StartInfo.RedirectStandardOutput = true; |
08 |
p.StandardInput.WriteLine(@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe " + |
10 |
p.StandardInput.WriteLine("sc start "+serviceName); |
11 |
p.StandardInput.WriteLine("exit"); |