Install a Windows service without InstallUtil.exe

I am trying to deploy a Windows service, but am not quite sure how to do this correctly. I created it as a console application to start with, now I turned it into a Windows service project and just call my class from the OnStart method in the service.

Now I need to install this on a server that does not have Visual Studio, which, if I understood correctly, means that I can not use InstallUtil.exe and should create an installer class instead. Is it correct?

I looked at the previous question Install the Windows.NET service without InstallUtil.exe , but I just want to make sure that I understood correctly.

If I create a class that has been answered back to, what is the next step? Upload MyService.exe and MyService.exe.config to the server, double-click the exe file and Bob is my uncle?

The service will be installed on only one server.

+31
c # install deployment windows-services
May 18 '10 at 11:22
source share
8 answers

The InstallUtil.exe tool is just a wrapper around reflection calls from the installer components (s) in your service. Thus, in fact, it does nothing but use the functions provided by these components of the installer. Marc Gravell's solution simply provides the tools for this from the command line, so you no longer need to rely on InstallUtil.exe on the target machine.

Here is my step by step based on the decision of Mark Gravell.

How to start the .NET Windows service immediately after installation?

+16
May 18 '10 at 13:49
source share

You can still use installutil without a visual studio, it is included in the .net infrastructure

On your server, open a command prompt as administrator, then:

 CD C:\Windows\Microsoft.NET\Framework\v4.0.version (insert your version) installutil "C:\Program Files\YourWindowsService\YourWindowsService.exe" (insert your service name/location) 

To delete:

 installutil /u "C:\Program Files\YourWindowsService\YourWindowsService.exe" (insert your service name/location) 
+28
May 08 '14 at 7:08
source share

I know this is a very old question, but it's better to update it with new information.

You can install the service using the sc command:

InstallService.bat:

 @echo OFF echo Stopping old service version... net stop "[YOUR SERVICE NAME]" echo Uninstalling old service version... sc delete "[YOUR SERVICE NAME]" echo Installing service... rem DO NOT remove the space after "binpath="! sc create "[YOUR SERVICE NAME]" binpath= "[PATH_TO_YOUR_SERVICE_EXE]" start= auto echo Starting server complete pause 

With SC, you can do much more: uninstall the old service (if you already installed it earlier), check if the service with the same name exists ... even configure the autorun service.

One of many links: creating a service using sc.exe; how to pass context parameters

I did so and InstallUtil . Personally, I feel that using SC is cleaner and better for your health.

+26
Dec 11 '15 at 10:11
source share

Why not just create an installation project? It is very easy.

  • Add the installation service to the service (you do this on the surface of the seemingly useless "design" service)
  • Create an installation project and add the service output to the settings application folder
  • The most important thing is to add the conclusion of the Service project to all user actions

Howled, and you're done.

See here: http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

There is also a way to request user credentials (or provide your own).

+5
May 18 '10 at 11:31
source share

This is the base class of the service (a subclass of ServiceBase) that can be subclassed to create a Windows service that can be easily installed from the command line without installutil.exe. This solution is obtained from How to start the Windows Windows service immediately after installation? by adding some code to get the type of service using the calling stackframe

 public abstract class InstallableServiceBase:ServiceBase { /// <summary> /// returns Type of the calling service (subclass of InstallableServiceBase) /// </summary> /// <returns></returns> protected static Type getMyType() { Type t = typeof(InstallableServiceBase); MethodBase ret = MethodBase.GetCurrentMethod(); Type retType = null; try { StackFrame[] frames = new StackTrace().GetFrames(); foreach (StackFrame x in frames) { ret = x.GetMethod(); Type t1 = ret.DeclaringType; if (t1 != null && !t1.Equals(t) && !t1.IsSubclassOf(t)) { break; } retType = t1; } } catch { } return retType; } /// <summary> /// returns AssemblyInstaller for the calling service (subclass of InstallableServiceBase) /// </summary> /// <returns></returns> protected static AssemblyInstaller GetInstaller() { Type t = getMyType(); AssemblyInstaller installer = new AssemblyInstaller( t.Assembly, null); installer.UseNewContext = true; return installer; } private bool IsInstalled() { using (ServiceController controller = new ServiceController(this.ServiceName)) { try { ServiceControllerStatus status = controller.Status; } catch { return false; } return true; } } private bool IsRunning() { using (ServiceController controller = new ServiceController(this.ServiceName)) { if (!this.IsInstalled()) return false; return (controller.Status == ServiceControllerStatus.Running); } } /// <summary> /// protected method to be called by a public method within the real service /// ie: in the real service /// new internal void InstallService() /// { /// base.InstallService(); /// } /// </summary> protected void InstallService() { if (this.IsInstalled()) return; try { using (AssemblyInstaller installer = GetInstaller()) { IDictionary state = new Hashtable(); try { installer.Install(state); installer.Commit(state); } catch { try { installer.Rollback(state); } catch { } throw; } } } catch { throw; } } /// <summary> /// protected method to be called by a public method within the real service /// ie: in the real service /// new internal void UninstallService() /// { /// base.UninstallService(); /// } /// </summary> protected void UninstallService() { if (!this.IsInstalled()) return; if (this.IsRunning()) { this.StopService(); } try { using (AssemblyInstaller installer = GetInstaller()) { IDictionary state = new Hashtable(); try { installer.Uninstall(state); } catch { throw; } } } catch { throw; } } private void StartService() { if (!this.IsInstalled()) return; using (ServiceController controller = new ServiceController(this.ServiceName)) { try { if (controller.Status != ServiceControllerStatus.Running) { controller.Start(); controller.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10)); } } catch { throw; } } } private void StopService() { if (!this.IsInstalled()) return; using (ServiceController controller = new ServiceController(this.ServiceName)) { try { if (controller.Status != ServiceControllerStatus.Stopped) { controller.Stop(); controller.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10)); } } catch { throw; } } } } 

All you have to do is implement two public / internal methods in your real service:

  new internal void InstallService() { base.InstallService(); } new internal void UninstallService() { base.UninstallService(); } 

and then call them when you want to install the service:

  static void Main(string[] args) { if (Environment.UserInteractive) { MyService s1 = new MyService(); if (args.Length == 1) { switch (args[0]) { case "-install": s1.InstallService(); break; case "-uninstall": s1.UninstallService(); break; default: throw new NotImplementedException(); } } } else { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new MyService() }; ServiceBase.Run(MyService); } } 
+2
Jul 11 '16 at 13:50
source share

Don’t double-click, you will run it with the correct command line parameters, so type something like MyService -i and then MyService -u to remove it.

Otherwise, you can use sc.exe to install and uninstall it (or copy by InstallUtil.exe).

0
May 18 '10 at 11:27
source share

Topshelf is an OSS project that was launched after answering this question, and it makes the Windows service a lot easier. I highly recommend looking into it.

http://topshelf-project.com/

0
Jan 27 '16 at 23:07 on
source share

This problem arises because of security, it is better to open the developer command line for VS 2012 in RUN AS ADMINISTRATOR and install your service, it will fix your problem.

0
Apr 29 '16 at 12:46 on
source share



All Articles