[轉貼]寫自動安裝的Windows服務(不需要安裝命令InstallUtil)

Once you have your service written, you'll need to add an installer class. This should derive from System.Configuration.Install.Installer. In this class's constructor, you need to add a System.ServiceProcess.ServiceProcessInstaller to your Installers collection. This will install the process your service lives in. Set its Username and Password properties to match the username and password of the account you'd like the process to run as.


You'll also need to add an installer for the service itself. The class that does this is System.ServiceProcess.ServiceInstaller. The only thing you need to do to it before adding it to the Installers collection is to set the ServiceName property. IMPORTANT! This must match the ServiceName you used in your ServiceBase-derived class.


The code that does all this looks like this:


using System;
using System.Configuration.Install ;
using System.ServiceProcess ;
using System.ComponentModel ;


public class MyInstaller : Installer
{
  public MyInstaller ()
  {
     ServiceProcessInstaller spi = new ServiceProcessInstaller ();
     spi.Username = "ISENGARD// CheckURL ";
     spi.Password = "ihsxa9up";


     ServiceInstaller si = new ServiceInstaller ();
     si.ServiceName = " CheckURL ";


     this .Installers.Add ( spi );
     this .Installers.Add ( si );
  }
}


If you've done installers before, you know this is the same code that the wizard will write for you. The only difference is that InstallUtil (the usual way of doing things) relies on the presence of the [RunInstaller(true)] attribute on the installer class. Adding won't hurt, but we don't need it so I've left it out.


The next thing we need to do is get the classes we've inherited from to do all the work. You can put this next bit of code anywhere you like, but I sort of like the idea of my service being self-installing, so I've added code to my Main method to look for the /install and /uninstall command-line switches, and to act appropriately.


"Appropriately" in this case means instantiating an instance of the TransactedInstaller class, adding our installer to its Installers collection, and setting a few things up. One of these is to tell the installer the path to the assembly we're installing. We can retrieve this using the Location property of the Assembly class. Oddly, the way the installation stuff wants this information is for us to pass it in an array of strings that contain what are essentially command line parameters. My guess is that the installer classes were built to work with InstallUtil in mind.


The other thing we need to do is pass in an empty Hashtable if we're calling Install. The installer will use this to store some things internally. We don't need to worry about that. When calling Uninstall, we just pass null. Other than that, the code for both cases is pretty similar.


Here's the code:


public class MyService : ServiceBase
{
  // Service stuff omitted for brevity

  static void Main( string [] args )
  {
    string opt = null ;
    if ( args.Length > 1)
    {
      opt = args [0];
    }
  
    if (opt != null && opt.ToLower () == "/install")
    {
       TransactedInstaller ti = new TransactedInstaller ();
       MyInstaller mi = new MyInstaller ();
       ti.Installers.Add (mi);
      String path = String.Format ("/ assemblypath ={0}",
         System.Reflection.Assembly.GetExecutingAssembly ().Location);
      String[] cmdline = {path};
       InstallContext ctx = new InstallContext ("", cmdline );
       ti.Context ( ctx );
       ti.Install ( new Hashtable ());
    }
    else if (opt != null && opt.ToLower () == "/uninstall")
    {
       TransactedInstaller ti = new TransactedInstaller ();
       MyInstaller mi = new MyInstaller ();
       ti.Installers.Add (mi);
      String path = String.Format ("/ assemblypath ={0}",
       System.Reflection.Assembly.GetExecutingAssembly ().Location);
      String[] cmdline = {path};
       InstallContext ctx = new InstallContext ("", cmdline );
       ti.Context ( ctx );
       ti.Uninstall ( null );
    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章