开发者

Why does the Component Designer generate a service installer object that it does not (appear to) use?

Following the steps detailed in http://msdn.microsoft.com/en-us/library/zt39148a.aspx#Y684, I have created a Windows service which I have successfully installed and tested. But I am puzzled by something in the components used to install the service.

Installation of the service is accomplished by a designer-generated ProjectInstaller class, shown below. This class is used by installutil.exe (in .NET Framework) to install the ser开发者_运维百科vice and its associated service process. The designer-generated code creates two installer objects: MyServiceInstaller for the service, and MyServiceProcessInstaller for the service process, to be used by installutil.exe at installation time. However, it inserts only the latter into the Installers collection.

So how does the service itself get installed? Is there a "default service" for the service process, if no services are explicitly installed along with the service process?

CODE:

(If you experiment with this, you will find that the Designer actually creates this class using two partial-classes. I have consolidated these into a single class for simplicity.)

namespace MyService
{
    [RunInstaller(true)]
    public class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Required method for Designer support - do not modify
         /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.MyServiceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.MyServiceInstaller = new System.ServiceProcess.ServiceInstaller();
            // 
            // MyServiceProcessInstaller
            // 
            this.MyServiceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.MyServiceProcessInstaller.Password = null;
            this.MyServiceProcessInstaller.Username = null;
            // 
            // MyServiceInstaller
            // 
            this.MyServiceInstaller.ServiceName = "MyService";
            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            // *** Expected here:  this.MyServiceInstaller,
            this.MyServiceProcessInstaller});
        }

        private System.ServiceProcess.ServiceProcessInstaller MyServiceProcessInstaller;
        private System.ServiceProcess.ServiceInstaller MyServiceInstaller;
    }
}


The ServiceInstaller does need to be added to the collection of Installers. I followed the same steps described in the article and it came out correctly:

private void InitializeComponent()
{
   this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
   this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
   // 
   // serviceProcessInstaller1
   // 
   this.serviceProcessInstaller1.Password = null;
   this.serviceProcessInstaller1.Username = null;
   // 
   // serviceInstaller1
   // 
   this.serviceInstaller1.ServiceName = "Service1";
   // 
   // ProjectInstaller
   // 
   this.Installers.AddRange(new System.Configuration.Install.Installer[] {
      this.serviceProcessInstaller1,
      this.serviceInstaller1});

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜