28

The only way to install windows-service I know is using "Visual Studio 2008 Command Prompt", Is there a way to install windows-service on a machine which isn't having Visual Studio installed (assume that .Net version 2.X is installed.)

Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114

12 Answers12

44

you can do it with installUtil which resides in the .net framework folder (on my machine it's C:\Windows\Microsoft.NET\Framework\v2.0.50727).

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • 4
    For my .Net 4.5 service I had to use the InstallUtil.exe located in C:\Windows\Microsoft.NET\Framework\v4.0.30319. When I tried to use the one in the v2.0.50727 directory I got an error saying "The assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded." – deadlydog Oct 01 '14 at 20:23
  • @deadlydog The original question was regarding .Net version 2, but you are correct that you need to use the version of `installUtil` that matches the framework version that the service is built for. – Klaus Byskov Pedersen Oct 02 '14 at 08:49
  • @giorgi It will depend which .Net framework you set your project to target. If your project targets .Net 4.0, but the server only has .Net 3.5 installed, then you won't be able to use installUtil for it, and even if you could get it installed, it wouldn't run. The server needs to at least have the .Net version (or greater) that your project is set to target. – deadlydog Jun 11 '15 at 15:27
8

You can install Windows Services as an MSI. Create the MSI as a normal Project attached to your Service Project.

In your Service Project Create an Installer Class and add the components serviceInstaller and serviceProcessInstaller and configure as required. Add the Project Output from the Service Project to the MSI Project, Set the Custom Actions of the MSI Project for Install, Commit, Rollback and Uninstall to the Output of the Service Project.

Alter the MSI Project in any other way as desired, build the MSI, then you will be able to install your Service to any Windows machine without VS.NET or by using the InstallUtil utility application. The MSI will also handle the Uninstall etc.

There are properties of the serviceProcessInstaller component which you can set for the Account to run the Service under, the serviceInstaller contains properties which can be set such as the Service Name. You will have to set the ServiceName and its Parent, MSDN will provide good information on how to use both of these components.

Dharman
  • 30,962
  • 25
  • 85
  • 135
7

Relevant MSDN page: http://msdn.microsoft.com/en-us/library/sd8zc8ha%28VS.80%29.aspx

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe yourproject.exe

You don't need to have Visual Studio installed for this to work.

Tiberiu Ana
  • 3,663
  • 1
  • 24
  • 25
5

Yep, installutil.exe works nicely, but I got tired of having to remember to start the Visual Studio 2008 Command Prompt in order to use it. Invariably, I'd open an ordinary console only to realize that the installutil.exe location was not part of the path.

Fortunately, the installutil.exe is a wrapper around a set of reflection calls. From MSDN,

Installutil.exe uses reflection to inspect the specified assembly and find all Installer types with the RunInstallerAttribute set to true. The tool then executes either the Install Method or the Uninstall Method on each instance of the Installer type.

What this means is that it is possible to add code to the Windows service to have it install and uninstall itself. By doing so, I no longer require installutil.exe or the Visual Studio 2008 Command Prompt to run it.

I have provided a step-by-step guide for how to do this here. The -install option installs the Windows service and starts it. Conversely, the -uninstall option stops the service and uninstalls it.

Community
  • 1
  • 1
Matt Davis
  • 45,297
  • 16
  • 93
  • 124
3

With visual studio command prompt:

installutil /i fullfilepathtoservice.exe

Without visual studio command prompt:

C:\WINNT\Microsoft.NET\Framework\v1.1.4322 installutil /i fullfilepathtoservice.exe

HTH.

Sunny
  • 6,286
  • 2
  • 25
  • 27
2

Open a command prompt (cmd) and type

help sc

and

sc create

In additon the installutil.exe you're probably talking about is really just in the .Net Framework directory. The command prompt of VS just sets up a nice path, but you can launch it from (my path) C:\Windows\Microsoft.NET\Framework\v2.0.50727 just fine.

Benjamin Podszun
  • 9,679
  • 3
  • 34
  • 45
2

You don't need the Visual Studio 2008 Command Prompt. You need the InstallUtil.exe program which can be found in

C:\Windows\Microsoft.NET\Framework\v2.0.50727

On x64 machines there's also a 64-bit version in

C:\Windows\Microsoft.NET\Framework64\v2.0.50727

These directories are obviously only available when .NET is installed.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
2

All that's doing is putting the .Net framework tools onto the PATH; you can go still use: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe by specifying the full path on a normal command prompt (obviously adjust for your framework / CPU architecture)

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
1

I guess that you're looking for the "installutil.exe" program, which one uses to install .NET Windows Services. You can find it in %windir%\Microsoft.NET\Framework\v2.0.50727

Daniel Chambers
  • 1,645
  • 16
  • 25
1

step 1> Install .net framework (I am using .net 4.0 but you can use as your requirement)

step 2> Go to run + cmd then click ok or directly go to your window command prompt (not visual studio command prompt )

step 3> type these lines

> cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe
> E:\test\test\bin\Debug\test.exe

E:\test\test\bin\Debug\ is the path where my window service setup is saved

C:\Windows\Microsoft.NET\Framework\v4.0.30319\ is path where InstallUtil.exe exist.

In both InstallUtil.exe exist. and path must give a space. Otherwise, it raises an error.

Step 4> Enjoy..your service is installed...now you can check it from control panel > administrative tool >Services.

piet.t
  • 11,718
  • 21
  • 43
  • 52
sisindrymedagam
  • 179
  • 2
  • 8
0

Run CMD as admin

Go to

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>

RUN the following:

InstallUtil C:\Dev\Codebase..\Main\Source....\bin\Debug\.exe

Paul George
  • 45
  • 2
  • 9
0

I've found that you can't do this programmically using .Net Core unless you create installers but then its not the program that's installing itself.

But this workaround works using System.Diagnostics.Process. Create a new process that calls your favourite sc.exe installation or uninstall command.

i.e.

void Install ()
{
    if (Environment.UserInteractive)
    {
        using (var process = new Process())
        {
            process.StartInfo.FileName = "sc.exe";
            process.StartInfo.Arguments = "";
            process.StartInfo.Verb = "runas";
            process.OutputDataReceived += Process_OutputDataReceived;
            process.Start();
            process.WaitForExit();
            // use ServiceController from System.ServiceProcess to start, configure the service
        }

        Environment.Exit(0);
    }
    else
    {
        // We can't do this as a service...
        Environment.Exit(2);
    }
}
AndyB
  • 101
  • 4