考虑到部署方便,我们一般都会将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. java 线程Thread 技术--线程状态与同步问题

    线程技术第三篇: 线程的状态: 1. 创建状态: 当用new 操作符创建一个新的线程对象时,该线程就处于创建状态,系统不为它分配资源 2.可运行状态:当线程调用start 方法将为线程分配必须的系统资 ...

  2. delete,truncate 和 delete之间的区别

    1.首先看下语法定义: drop table_name truncate table_name delete table_name [where column_name = value] 2.各个删除 ...

  3. 10.23JS日记

    1.逻辑运算 ||  &&  ! ||:遇到第一个为true的值就中止并返回 &&:遇到第一个为false的值就中止并返回,如果没有false值,就返回最后一个不是fa ...

  4. 26.mysql日志

    26.mysql日志mysql日志包括:错误日志.二进制日志.查询日志.慢查询日志. 26.1 错误日志错误日志记录了mysqld启动到停止之间发生的任何严重错误的相关信息.mysql故障时应首先查看 ...

  5. PAT 1081 检查密码(15) (代码+思路)

    1081 检查密码(15 分) 本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能.该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母.数字和小数点 .,还必须既有字母也 ...

  6. 转录本组装软件StringTie的使用说明

    转录本组装软件StringTie的使用说明 StringTie 转录本组装软件StringTie的使用说明 转录组分析流程 HISTA + StringTie 组合.其Protocol 发表在Natu ...

  7. postfix 如何设置邮件头翻译的功能

    开始按http://semi-legitimate.com/blog/item/how-to-rewrite-outgoing-address-in-postfix 博客中的方法进行设置,是可以替换, ...

  8. Codeforces 757B. Bash's Big Day GCD

    B. Bash's Big Day time limit per test:2 seconds memory limit per test:512 megabytes input:standard i ...

  9. python 打开文件对话框 filedialog tkinter GUI 编程

    - -读取文件的gui编程 # _*_ coding:utf-8 _*_ import tkinter from tkinter import filedialog def openfiles2(): ...

  10. 将某视图View转换为UIImage

    + (UIImage *)getSharedScreenView{ UIWindow *screenWindow = [[UIApplication sharedApplication]keyWind ...