zoukankan      html  css  js  c++  java
  • [转].Net Windows服务安装完成后自动启动

    本文转自:http://www.cnblogs.com/hb_cattle/archive/2011/12/04/2275319.html

    考虑到部署方便,我们一般都会将C#写的Windows服务制作成安装包。在服务安装完成以后,第一次还需要手动启动服务,这样非常不方便。查阅了网上的一些资料,发现有一种方法是在安装完成事件里面调用命令行的方式启动服务,这种方式虽可行,但觉得不够完美。好了,下面来看看如何更好地做到服务自动启动。

        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);
                //Auot start service after the installation is completed
                ServiceController sc = new ServiceController("CleanExpiredSessionSeivice");
                if (sc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    sc.Start();
                }
            }
        }
    }
    

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

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

    image

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

    image

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

    作者:陈 锋
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Kudu-Impala集成特性
    [转]IIS的各种身份验证详细测试
    [转]The NTLM Authentication Protocol and Security Support Provider
    [转]WxEmojiView
    [转]Redis 数据类型
    [转]a-mongodb-tutorial-using-c-and-asp-net-mvc
    [转]MongoDB 概念解析
    [转]flash.net.Socket
    [转]emailjs-smtp-client
    [转]jquerUI Dialog中隐藏标题栏的关闭"X"按钮
  • 原文地址:https://www.cnblogs.com/freeliver54/p/7052822.html
Copyright © 2011-2022 走看看