Showing posts with label WinXP. Show all posts
Showing posts with label WinXP. Show all posts

Sunday, June 14, 2009

Programmatically Install Window Service

This article is about to install / uninstall window service programmatically from C#. This is useful when you need to install / uninstall window service from some other application.

For this purpose you need to use ServiceProcessInstaller and ServiceInstaller class from another application. These two installer classes are responsible for Installation of service.

C# Example for Programmatically Install Window Service.

ServiceProcessInstaller ProcesServiceInstaller = new ServiceProcessInstaller();
ProcesServiceInstaller.Account =  ServiceAccount.User;
ProcesServiceInstaller.Username = "<<username>>";
ProcesServiceInstaller.Password = "<<password>>";

ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new System.Configuration.Install.InstallContext();
String path = String.Format("/assemblypath={0}", @"<<path of executable of window service>>");
String[] cmdline = { path };

Context = new System.Configuration.Install.InstallContext("", cmdline);              
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.DisplayName = "MyService";
ServiceInstallerObj.Description = "MyService installer test";
ServiceInstallerObj.ServiceName = "MyService";
ServiceInstallerObj.StartType =  ServiceStartMode.Automatic;
ServiceInstallerObj.Parent = ProcesServiceInstaller;          

System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
ServiceInstallerObj.Install(state);

C# Example to Programmatically Uninstall Window Service.

ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
InstallContext Context = new InstallContext("<<log file path>>", null);
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = "MyService";
ServiceInstallerObj.Uninstall(null);

other article related to window service.

http://dotnetstep.blogspot.com/2009/06/passing-parameter-to-installutil.html

http://dotnetstep.blogspot.com/2009/06/install-window-service-without-using.html

http://dotnetstep.blogspot.com/2009/06/install-windowservice-using-installutil.html

http://dotnetstep.blogspot.com/2009/06/get-windowservice-executable-path-in.html

Please give your idea about this article.

Saturday, June 13, 2009

Passing Parameter to InstallUtil

When installing window service or any installer ( class inherits from install class) required custom parameter at run time , Need to pass as parameter of installutil.

Crating window service using article (http://dotnetstep.blogspot.com/2009/06/install-windowservice-using-installutil.html).  This article suggest you add installer ( ProjectInstaller). Now we use installer class events to configure ServiceProcessInstaller and ServiceInstaller as per parameter pass to installutil.

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
           this.BeforeInstall += new InstallEventHandler(ProjectInstaller_BeforeInstall);
            this.BeforeUninstall += new InstallEventHandler(ProjectInstaller_BeforeUninstall);
        }       

        void ProjectInstaller_BeforeInstall(object sender, InstallEventArgs e)
        {
            // Configure Account for Service Process.
            switch(this.Context.Parameters["Account"])
            {
                case "LocalService":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
                    break;
                case "LocalSystem":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                    break;
                case "NetworkService":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.NetworkService;
                    break;
                case "User":
                    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
                    this.serviceProcessInstaller1.Username = this.Context.Parameters["UserName"];
                    this.serviceProcessInstaller1.Password = this.Context.Parameters["Password"];
                    break;
            }      
            // Configure ServiceName
            if(!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"]))
            {               
                this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];
            }
        }

        void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e)
        {
            if (!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"]))
            {
                this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"];
            }
        }
    }

Above ProjectInstaller class handle two events : BeforeInstall and BeforeUninstall. BeforeInstall is called just before install method of installer (ProjectInstaller) called via InstallUtil and same way BeforeUninstall called just before Uninstall method of projectinstaller class. After adding that two event handler use following to configure service using installutil.

1. Install
installutil /Account=User /UserName=admin /Password=admin /ServiceName=WinService1 /i WindowService1.exe

2. Uninstall
installutil /ServiceName=WinService1 /u WindowService1.exe

This will also help you configure WindowService1.exe with two different name and different account.
installutil /Account=User /UserName=admin /Password=admin        /ServiceName=WinService1 /i WindowService1.exe

installutil /Account=LocalSystem /ServiceName=WinService2 /i WindowService1.exe

Install window service without using installutil

Article is about to install window service without using InstallUtil utility.
Windows XP or Later has command line utility called sc . (This utility talks with service controller and with services from command line).
SC is a window base utility. AS explain in article http://dotnetstep.blogspot.com/2009/06/install-windowservice-using-installutil.html sc does not required ProjectInstaller.

1. Start service using sc
          sc start ServiceName

2. Stop serivce
         sc stop ServiceName

3. Delete window service
        sc delete servicename
        Note : During delete call if service is in running state then service is delete when nexttime service is stop or PC restart.

4. Create window service
       sc create ServiceName binpath= “c:\windowservice1\windowservice1.exe”

       Apart from above many option available . For that just go to command prompt and type sc create /?

  For example configure service running account.

sc create servicename binpath= “c:\windowservice1\windowservice1.exe” obj= administrator password= pass

Hope this article will help you .

Please give your comment or idea about this.

In above all case Service ServiceName ( Mark as blue) , don’t use display name.

 image

Saturday, May 16, 2009

SMTP Setting to send outbound email

Here i am going to explain about steps required to configure SMTP (Local SMTP) to send outbound email.

Steps.
1. Go to run and Type inetmgr. After inetmgr ( Internet information services manager ) open. Right click on Default SMTP virtual server ( As shown in figure 1) . If this is not displayed in your inetmgr then you need to first install SMTP services using Add/ Remove component.
 1

2. After right click on Default SMTP virtual server go for property 
menu item. It will display as show in following image.
image
3. Now go in Access Tab and click on relay. Please select as shown in following image and click ok.
 2
4. After that go for delivery tab. ( Display as follows)
 image

5. Click on outbound security . In that tab select basic authentication. Here i am using by gmail account to send outbound email. AS gmail required TLS (security to transfer credential) , you have to select TLS encryption checkbox too.Then click on ok.
5
7. Now go for outbound connection button. It will display as follows. Please specify TCP port as 587 for gmail otherwise 25 as default.
3

8. Click on Advanced . Configure as follows . In case of other SMTP , you have to specify that smtp server.

4

Click ok and then finally click on apply. Now restart smtp server using inetmgr.
Note : Here i took Gmail smtp setting . For that you need to configure Gmail account from Gmail setting and click on IMAP enable.

Thursday, April 2, 2009

How to get assembly name for registration ?

When assembly added into global assembly cache and there is need for adding that assembly to web.config assemblies section.

<compilation debug="false">
          <assemblies>
            <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            </assemblies>
</compilation>

Now there is need to add System.Web.Extensions assembly , for that you need assembly detail with its public key token to register.

Easiest way to get that detail is 

1. Go to run and type assembly.

run

it will open following window.

window1

2. Now select assembly , for example system.web.extensions , then go to edit menu and select option Copy display name as shown in following image.

window2

3. Now paste data.

image

you get all detail to register assembly. Enjoy… !

Friday, March 6, 2009

Run MSI In Context Of User

Several times to install program you need administrative or other user privileges. For example you take input from user for username and password and run that program in that particular user context.

I faced problem during installing MSI in particular user context or start IIS in context of another user.

I found this solution using command line argument.

1. Inermgr ( Run following command in Command line)

C:\runas /user:administrator “mmc %sysroot%\system32\inetsrv\iis.msc”
Enter the password for administrator :

2. MSI install
C:\runas /user:administrator “msiexec /i <full path of msi file>”

c:\runas /user:administrator “msiexec /i c:\download\setup.msi”
Enter the password for administrator :

For more information just type runas /? on command line.

Wednesday, February 11, 2009

Kill Process On Remote Machine

It is not recommended that each and every time abort process this way but sometime it is necessary stop some process from executing on remote machine.

There are few useful command for that.

TaskList
Taskkill or tskill

Run following command on command prompt.
> TaskList     ( Current system running process list)

> TaskList /S <system name>
    e.g Remote system name remote1 then
   TaskList /S remote1
   Some of the case you require to pass credential for remote pc in that case use following command.
   TaskList /S remote1 /U <username> /P <Password>
TaskList command is used to get process id that we want to kill.

To Kill process there are two command TaskKill and tskill . I prefer TaskKill over tskill as it gives more command line options.

> TaskKill /PID <process id> ( use to kill perticular process that identified by process id on local machine)

> Taskkill /S <remote pc name or IP Address> /PID <process id> ( kill process that identified by process id on remote machine)

If remote pc require credential than use

> taskkill /S < remote pc name or IP Address> /PID <process id> /U <username> /P <password>

All above command are tested on Windows XP and Windows 2003.
You must require permission to run above command in order to use functionality.

In All Above you can use IP Address instead of remote PC name.