考虑到部署方便,我们一般都会将C#写的Windows服务制作成安装包。在服务安装完成以后,第一次还需要手动启动服务,这样非常不方便。

方法一:在安装完成事件里面调用命令行的方式启动服务

此操作之前要先设置下两个控件

设置serviceProcessInstaller1控件的Account属性为“LocalSystem”
设置serviceInstaller1控件的StartType属性为"Automatic"

 
在服务器上添加安装程序,在private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)事件中,添加以下代码:
 
[csharp] view plaincopy

  1. /// <summary>
  2. /// 安装后自动启动服务
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
  7. {
  8. Process p = new Process
  9. {
  10. StartInfo =
  11. {
  12. FileName = "cmd.exe",
  13. UseShellExecute = false,
  14. RedirectStandardInput = true,
  15. RedirectStandardOutput = true,
  16. RedirectStandardError = true,
  17. CreateNoWindow = true
  18. }
  19. };
  20. p.Start();
  21. const string cmdString = "sc start 银医通服务平台1.0"; //cmd命令,银医通服务平台1.0为服务的名称
  22. p.StandardInput.WriteLine(cmdString);
  23. p.StandardInput.WriteLine("exit");
  24. }

查阅了网上的一些资料,这种方式虽可行,但觉得不够完美。好了,下面来看看如何更好地做到服务自动启动。

方法二:使用ServiceController对象

1.重写ProjectInstaller的Commit方法

[csharp] view plaincopy

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration.Install;
  6. using System.Linq;
  7. using System.ServiceProcess;
  8. namespace CleanExpiredSessionSeivice
  9. {
  10. [RunInstaller(true)]
  11. public partial class ProjectInstaller : System.Configuration.Install.Installer
  12. {
  13. public ProjectInstaller()
  14. {
  15. InitializeComponent();
  16. }
  17. public override void Commit(IDictionary savedState)
  18. {
  19. base.Commit(savedState);
  20. ServiceController sc = new ServiceController("银医通服务平台1.0");
  21. if(sc.Status.Equals(ServiceControllerStatus.Stopped))
  22. {
  23. sc.Start();
  24. }
  25. }
  26. }
  27. }

2、在服务安装项目中添加名为 Commit的 Custome Action

在服务安装项目上右击,在弹出的菜单中选择View — Custom Actions

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

需要注意的是,第二步操作是必不可少的,否则服务无法自动启动。我的个人理解是Commit Custom Action 会自动调用ProjectInstaller的Commit方法,Commit Custom Action 在这里扮演了一个调用者的角色。

.Net实现Windows服务安装完成后自动启动的两种方法的更多相关文章

  1. Windows服务安装完成后自动启动

    public ServiceInstaller() { //... Installer code here this.AfterInstall += new InstallEventHandler(S ...

  2. [转].Net Windows服务安装完成后自动启动

    本文转自:http://www.cnblogs.com/hb_cattle/archive/2011/12/04/2275319.html 考虑到部署方便,我们一般都会将C#写的Windows服务制作 ...

  3. centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课

    centos  lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress  安装phpmyadmin  定时备份mysql两种方法  第二十五节 ...

  4. Win8.1RTM英文版安装中文语言包的两种方法

    Windows 8.1和Windows Server 2012 R2 RTM出来了,下载了个英文版的安装上了,发现远景上有朋友提供中文语言包,以下提供两种语言包的安装方法: 一.使用lpksetup命 ...

  5. windows 7中添加新硬件的两种方法(本地回环网卡)

    最近在windows7上使用VMwareWorkstation7玩一些实验,遇到需要配置不同网络的问题. 因为在windows2003server上习惯使用要本地回环网卡了,那就想着在Windows7 ...

  6. pip安装使用国内源的两种方法

    pip安装后使用pip安装第三方库默认是国外源,一般安装慢连接不稳定,等得花儿都谢了,结果还告诉你安装失败..../(ㄒoㄒ)/~~ 这时我们就要想想其它办法啦,毕竟不能强求 国外不行,就只有国内了赛 ...

  7. iManager微服务自定义上传数据的两种方法

    背景 当数据量大时,通过浏览器上传可能速度较慢,可以使用以下两种方式进行数据导入. 方法一 1.访问k8s主页(31234端口) 找到所创建的微服务的命名空间(例如icloud-native-xxx) ...

  8. sublime 快速安装多个插件的两种方法[Advanced Install Package]与[Package Control.sublime-settings]

    环境 sublime 3,目前官网下载的sublime3 已经支持自动安装 Package Control . 第一种: ctrl+shift+p 调出命令面板以后 以前都是使用 Package Co ...

  9. 学习windows编程 day3 之 自定义画笔的两种方法

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRU ...

随机推荐

  1. mysql系列(3)之 DML语句

    DML操作指的是对数据库中表记录的操作,主要包括表记录的插入(insert).更新(update).删除(delete)和查询(select). 1.插入

  2. echarts折线图Demo

    echarts链接:http://echarts.baidu.com/examples/editor.html?c=line-stack 黑底代码:http://gallery.echartsjs.c ...

  3. shell脚本计算斐波那契数列

    计算斐波那契数列 [1,1,2,3,5,8,,,,,] #!/bin/bash n=$ num=( ) i= while [[ $i -lt $n ]] do let num[$i]=num[$i-] ...

  4. 同种类型不同名字的变量在for循环中操作

    InfoViewController * info = [[InfoViewController alloc] init]; HeroViewController * hero = [[HeroVie ...

  5. Python爬虫项目--爬取某宝男装信息

    本次爬取用到的知识点有: 1. selenium 2. pymysql 3  pyquery 正文 1. 分析目标网站 1. 打开某宝首页, 输入"男装"后点击"搜索&q ...

  6. subprocess.Popen在win10下会有异常

    win10运行下 会报740错误 查了下搜索结果 是uac问题 但uac已经是关闭状态 后直接使用os.popen进行替换 运行ok

  7. 基于RBAC权限验证, 中间价middleware实现, views 登录视图代码

    废话不多说  上代码: 基础实现: rom django.shortcuts import HttpResponse, redirect, render from django.http import ...

  8. ecplise自动提示失效,使用补全自动提示快捷键(Alt+/),但只显示“No Default Proposals”

    在这里设置了自动提示,但是在使用的时候自动提示实现了.甚至使用补全自动提示快捷键(Alt+/),只显示“No Default Proposals”.今天在网上搜索了一下结果,主要有一下几种方法: 1. ...

  9. linux arm-linux-gcc 安装编译

    1,将  .tgz  安装包通过SSH传至ubuntu 2,tar -zxvf  arm-linux-gcc.tgz     解压 3,配置环境变量(由于鄙人只需其中一个用户使用,所以直接再其主目录) ...

  10. 模块二 hashlib模块、configparser模块、logging模块

    算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常 ...