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方法并在该方法中添加自己的代码

 

view source
print?
01 public partial class RemoteSample : ServiceBase
02 {
03     public RemoteSample()
04     {
05         InitializeComponent();
06     }
07     protected override void OnStart(string[] args)
08     {
09         //our code in here
10     }
11 }

 

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

示例代码如下:

 

view source
print?
01 using System;
02 using System.ComponentModel;
03 using System.Configuration.Install;
04 using System.Diagnostics;
05   
06 namespace RemoteSample.WindowsService
07 {
08     [RunInstaller(true)]
09     public partial class ProjectInstaller : Installer
10     {
11         public ProjectInstaller()
12         {
13             InitializeComponent();
14             this.AfterInstall += ProjectInstaller_AfterInstall;
15         }
16   
17         void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
18         {
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;                                                      //是否从StandardInput中读取输入
24             p.StartInfo.RedirectStandardOutput = true;                                                     //是否从StandardInput中读取输出
25             p.Start();
26             p.StandardInput.WriteLine("sc start "+new RemoteSample().ServiceName);
27             p.StandardInput.WriteLine("exit");                                                              //退出控制台
28             p.Dispose();
29         }
30     }
31 }

6.创建一个安装windows服务并启动的应用

   我们需要获取windows服务安装文件的地址(servicePath),并且我们必须知道windows服务的名称(serviceName)

   添加以下代码实现选取windows服务安装文件安装并启动

 

view source
print?
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;                                                      //是否从StandardInput中读取输入
06 p.StartInfo.RedirectStandardOutput = true;                                      
07 p.Start();
08 p.StandardInput.WriteLine(@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe " +
09     servicePath);
10 p.StandardInput.WriteLine("sc start "+serviceName);
11 p.StandardInput.WriteLine("exit");