New Message on dotNET User Group Hyd

Article :- Windows Services Part - 2

Reply
  Reply to Sender   Recommend Message 1 in Discussion
From: Nasha

Hey Group,

Yesterday we created our MoveFiles Windows Service. Today we will add installers to our windows service, install our service and then go ahead and debug our service.

To add installers to our service we need to go to Design mode of our MoveFileService.cs and then click on properties.

You will see that there is a link called Add Installer at the bottom of the properties window. Click on that link

Notice that a new class called as ProjectInstaller.cs should be added to your project. Go to the design mode of your ProjectInstaller class and checkout serviceProcessInstaller1 and serviceInstaller1 objects added to it.

ServiceProcessInstaller1 is an object of ServiceProcessInstaller and ServiceInstaller1 is an object of ServiceInstaller.

Checkout the properties of both esp. serviceInstaller1. Amongst all the property present, one of the properties is StartType which is set to Manual by default i.e. this service will have to be started manually. You can even set it to automatic or disabled. If you set it to automatic then your service will start as soon as the OS starts.

Let us now checkout our Installers in some more detail.

ProjectInstaller : This is our main Installer class. If you check out it is inherited from System.Configuration.Install.Installer the base class of all the Installer classes i.e. if you have to create your own custom installer it should be inherited from this class.
The installer base class has Install, commit,rollback and uninstall methods. You can override these methods if you want to do any sp. task but do not forget to call the baseclass method in your new overriding method.

Another thing that you need to take into account is the RunInstaller attribute at the top of the ProjectInstaller class. This attribute tells that whenever this assembly is to be installed ProjectInstaller should be involved. Hence if you are creating your custom installer than you should put this attribute at the top of your installer class for it to be invoked.

ServiceProcessInstaller :- This class installs executables which are derived from the ServiceBase class. One instance of this class is required per service application. This class has properties of service process and all the properties of the various services in that process. Mind you one service process can contain more that one services.

ServiceInstaller :- For each service in the service process one service installer is need. Since we have only one service we will require only one. If you add one more service to your project you will require another service installer class to install that service.


Let us now checkout the InitializeComponent function of ProjectInstaller class.

                private void InitializeComponent()
                {
                        this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
                        this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
                        //
                        // serviceProcessInstaller1
                        //
                        this.serviceProcessInstaller1.Password = null;
                        this.serviceProcessInstaller1.Username = null;


Here is a part of it. Checkout the first two lines. It is this Intitalize component which creates these installers within the ProjectInstaller class. After that you will notice the user name and password for this serviceprocess. By default they are set to null if you set them to any specific value then that username password will be used to install and run the service. If you leave it null (which is the default) then when you are installing this service you will be prompted for username and password using the ServiceInstallerDailog.

So now after we have added our installer class let us build our service.

One of the Tools that I had not covered in my .NET Tools Series is Installutil. The reason behind it being that I wanted to cover it with windows services by creating a windows service and using it as an example.

Go to VS.Net command prompt ... type installutil /? and checkout its properties.

To install our assembly we will have to go to our assembly folder on the command prompt and then type the following command.

        installutil <Name of your assembly>.exe

Note : If your assembly is not installed . or the installation process in rollbacked get back to me . I will help you out.
          To uninstall your assembly use installutil /u <Name of your assembly>.exe

After installing the service now we will debug our service.

Debugging a windows service is different from any other normal application. Since this runs as an external process you will have to attach the process to your service application.

Open your service application keep a break point on OnStart method of your MoveFilesService.

Now start your service by going to Control Panel , Adminsitrative Tools , Services . Select your service, right click and select start.

Immediately attach the process to your service application by going to Debug -> Processes -> Check Show system processes -> Select your windows service.  Click on Attach -> Click Ok -> Click on Close.

Note: It might take some time for the process to come to your break point. So please bea little patient.

There you are debugging your windows service.

Have a gr8 time doin it ..... ENJOY !!!!!

View other groups in this category.

Click here
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to