Showing posts with label Windows Service. Show all posts
Showing posts with label Windows Service. Show all posts

Wednesday, November 27, 2013

Self Restarting Windows Service.


I had an situation in my Windows Service. There was a connectivity issue when dealing with the Email server. Email Server connectivity was using 3rd party .dll. and it was making productivity down. So to bring it up only solution was to restart the Service manually. To fix it automatically we need to fix in the same service writing code to restart itself. But it is not so easy. Self stop and start is not possible with out the help of another process.

Solution 1:
Creating other process telling it to stop and start the service.


                 Process proc = new Process();
                 ProcessStartInfo psi = new ProcessStartInfo();

                 psi.CreateNoWindow = true;
                 psi.FileName = "cmd.exe";
                 psi.Arguments = "/C net stop SERVERNAME && net start SERVERNAME ";
                 psi.LoadUserProfile = false;
                 psi.UseShellExecute = false;
                 psi.WindowStyle = ProcessWindowStyle.Hidden;
                 proc.StartInfo = psi;
                 proc.Start();

Solution 2:
Call, Environment.Exit with an error code greater than 0, which seems appropriate. Then on install we configure the service to restart on error.

                 Environment.Exit(1);



Monday, June 20, 2011

How to install a Windows service using command line ?

How to install a Windows service using command line ?

1>> Make a build of the service project then go to bin directory and copy required build files from either of the directory Release / Debug.
2>> Service will be in .exe file. This should be installed now, by using a command line   as below –

Copy blow one to bat file , name it as install.bat.

c:
cd c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
InstallUtil.exe "E:\test\testservice.exe"
pause


Run install.bat to install the service.

3> To uninstall there are two ways –
               a)
c:
cd c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
InstallUtil.exe –u "E:\test\testservice.exe"
pause

b)
sc delete <serviceName>
pause

               Some time using point number 3.a  , it is required to restart the windows machine so better to use the point 3.b.



Note- if you struck with the service which is already installed then you can kill that process , either using command line or ctrl+Alt+del select service and kill the process.

Monday, January 24, 2011

Kill process id in windows - force windows service to stop from stopping status


Kill process id in windows - force windows service to stop from stopping status

You can retrieve the PID for the process of the service through the command

sc queryex <servicename>

it returns some information like this

NOME_SERVIZIO: <servicename>
TIPO : 10 WIN32_OWN_PROCESS
STATO : 3 STOP_PENDING
(STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
CODICE_USCITA_WIN32 : 0 (0x0)
CODICE_USCITA_SERVIZIO : 0 (0x0)
PUNTO_ARRESTO : 0x0
INDICAZIONE_ATTESA : 0x0
PID : 1824
FLAG :

Then you can stop the service by the command

taskkill /F /PID 1824 (the specific PID)


Friday, August 20, 2010

Error - Application popup: wzunzip.exe while using WinZip 14.5 command line in Window service.

Bug :Was not able extract any zip files( WinZip 14.5 ) through Window service application ( which was using WinZip 14.5  command line )

Description: An Information found in Event logger – System as
Application popup: wzunzip.exe - Application Error : The application failed to initialize properly (0xc0000142). Click on OK to terminate the application.

Resolved :
This can be resolved just setting up the property of your application window service as below .


By check – Allow service to interact with desktop , it is said to service allow popup windows but by default service will run in hidden / silent mode . So nothing to worry about the popup .

Using Winzip14.5 command line – wzunzip.exe
Code snippet, I used in my application testing .

 using System;  
 using System.Collections.Generic;  
 using System.Text;  
 using System.IO;  
 namespace UnZipTest  
 {  
   public class UnZip  
   {  
     public void Run (string path,string extractPath){  
       DirectoryInfo dirInfo = new DirectoryInfo(path);  
       System.Diagnostics.Process P = new System.Diagnostics.Process();  
       if(Directory.Exists(extractPath)==false){  
         Directory.CreateDirectory(extractPath);  
       }  
       string UnZipCmd = null;  
       foreach(FileInfo fl in dirInfo.GetFiles("*.zip")){  
         try  
         {  
           string password = "123";  
           string zipExtract = extractPath + "\\" + fl.Name + "_" + Guid.NewGuid().ToString().Substring(0, 4);  
           if (password.Trim().Length == 0)  
           {  
             UnZipCmd = string.Format("-o \"{0}\" \"{1}\"", fl.FullName, zipExtract);  
             P = System.Diagnostics.Process.Start(@"C:\Program Files\WinZip\wzunzip", UnZipCmd);  
           }  
           else{  
             UnZipCmd = string.Format("-s\"{0}\" -o \"{1}\" \"{2}\"",password, fl.FullName, zipExtract);  
             P = System.Diagnostics.Process.Start(@"C:\Program Files\WinZip\wzunzip", UnZipCmd);  
           }  
           P.WaitForExit();  
         }  
         catch (Exception ex){  
           throw ex;  
         }  
       }  
     }  
   }  
 }  

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code