.Net实现Windows服务安装完成后自动启动的两种方法
考虑到部署方便,我们一般都会将C#写的Windows服务制作成安装包。在服务安装完成以后,第一次还需要手动启动服务,这样非常不方便。
方法一:在安装完成事件里面调用命令行的方式启动服务
设置serviceProcessInstaller1控件的Account属性为“LocalSystem”
设置serviceInstaller1控件的StartType属性为"Automatic"
- /// <summary>
- /// 安装后自动启动服务
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
- {
- Process p = new Process
- {
- StartInfo =
- {
- FileName = "cmd.exe",
- UseShellExecute = false,
- RedirectStandardInput = true,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- CreateNoWindow = true
- }
- };
- p.Start();
- const string cmdString = "sc start 银医通服务平台1.0"; //cmd命令,银医通服务平台1.0为服务的名称
- p.StandardInput.WriteLine(cmdString);
- p.StandardInput.WriteLine("exit");
- }
查阅了网上的一些资料,这种方式虽可行,但觉得不够完美。好了,下面来看看如何更好地做到服务自动启动。
方法二:使用ServiceController对象
1.重写ProjectInstaller的Commit方法
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration.Install;
- using System.Linq;
- using System.ServiceProcess;
- namespace CleanExpiredSessionSeivice
- {
- [RunInstaller(true)]
- public partial class ProjectInstaller : System.Configuration.Install.Installer
- {
- public ProjectInstaller()
- {
- InitializeComponent();
- }
- public override void Commit(IDictionary savedState)
- {
- base.Commit(savedState);
- ServiceController sc = new ServiceController("银医通服务平台1.0");
- if(sc.Status.Equals(ServiceControllerStatus.Stopped))
- {
- sc.Start();
- }
- }
- }
- }
2、在服务安装项目中添加名为 Commit的 Custome Action
在服务安装项目上右击,在弹出的菜单中选择View — Custom Actions

然后在Commit项上右击,选择Add Custom Action…,在弹出的列表框中选择Application Folder。最终结果如下:

需要注意的是,第二步操作是必不可少的,否则服务无法自动启动。我的个人理解是Commit Custom Action 会自动调用ProjectInstaller的Commit方法,Commit Custom Action 在这里扮演了一个调用者的角色。
.Net实现Windows服务安装完成后自动启动的两种方法的更多相关文章
- Windows服务安装完成后自动启动
public ServiceInstaller() { //... Installer code here this.AfterInstall += new InstallEventHandler(S ...
- [转].Net Windows服务安装完成后自动启动
本文转自:http://www.cnblogs.com/hb_cattle/archive/2011/12/04/2275319.html 考虑到部署方便,我们一般都会将C#写的Windows服务制作 ...
- centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课
centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节 ...
- Win8.1RTM英文版安装中文语言包的两种方法
Windows 8.1和Windows Server 2012 R2 RTM出来了,下载了个英文版的安装上了,发现远景上有朋友提供中文语言包,以下提供两种语言包的安装方法: 一.使用lpksetup命 ...
- windows 7中添加新硬件的两种方法(本地回环网卡)
最近在windows7上使用VMwareWorkstation7玩一些实验,遇到需要配置不同网络的问题. 因为在windows2003server上习惯使用要本地回环网卡了,那就想着在Windows7 ...
- pip安装使用国内源的两种方法
pip安装后使用pip安装第三方库默认是国外源,一般安装慢连接不稳定,等得花儿都谢了,结果还告诉你安装失败..../(ㄒoㄒ)/~~ 这时我们就要想想其它办法啦,毕竟不能强求 国外不行,就只有国内了赛 ...
- iManager微服务自定义上传数据的两种方法
背景 当数据量大时,通过浏览器上传可能速度较慢,可以使用以下两种方式进行数据导入. 方法一 1.访问k8s主页(31234端口) 找到所创建的微服务的命名空间(例如icloud-native-xxx) ...
- sublime 快速安装多个插件的两种方法[Advanced Install Package]与[Package Control.sublime-settings]
环境 sublime 3,目前官网下载的sublime3 已经支持自动安装 Package Control . 第一种: ctrl+shift+p 调出命令面板以后 以前都是使用 Package Co ...
- 学习windows编程 day3 之 自定义画笔的两种方法
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...
随机推荐
- mysql系列(3)之 DML语句
DML操作指的是对数据库中表记录的操作,主要包括表记录的插入(insert).更新(update).删除(delete)和查询(select). 1.插入
- echarts折线图Demo
echarts链接:http://echarts.baidu.com/examples/editor.html?c=line-stack 黑底代码:http://gallery.echartsjs.c ...
- shell脚本计算斐波那契数列
计算斐波那契数列 [1,1,2,3,5,8,,,,,] #!/bin/bash n=$ num=( ) i= while [[ $i -lt $n ]] do let num[$i]=num[$i-] ...
- 同种类型不同名字的变量在for循环中操作
InfoViewController * info = [[InfoViewController alloc] init]; HeroViewController * hero = [[HeroVie ...
- Python爬虫项目--爬取某宝男装信息
本次爬取用到的知识点有: 1. selenium 2. pymysql 3 pyquery 正文 1. 分析目标网站 1. 打开某宝首页, 输入"男装"后点击"搜索&q ...
- subprocess.Popen在win10下会有异常
win10运行下 会报740错误 查了下搜索结果 是uac问题 但uac已经是关闭状态 后直接使用os.popen进行替换 运行ok
- 基于RBAC权限验证, 中间价middleware实现, views 登录视图代码
废话不多说 上代码: 基础实现: rom django.shortcuts import HttpResponse, redirect, render from django.http import ...
- ecplise自动提示失效,使用补全自动提示快捷键(Alt+/),但只显示“No Default Proposals”
在这里设置了自动提示,但是在使用的时候自动提示实现了.甚至使用补全自动提示快捷键(Alt+/),只显示“No Default Proposals”.今天在网上搜索了一下结果,主要有一下几种方法: 1. ...
- linux arm-linux-gcc 安装编译
1,将 .tgz 安装包通过SSH传至ubuntu 2,tar -zxvf arm-linux-gcc.tgz 解压 3,配置环境变量(由于鄙人只需其中一个用户使用,所以直接再其主目录) ...
- 模块二 hashlib模块、configparser模块、logging模块
算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常 ...