Stack Exchange

sign up | log in | careers 2.0 |

Stack Overflow

Questions

Tags

Tour

Users

Ask Question

Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Install a .NET windows service without InstallUtil.exe

up vote 90 down vote favorite
60

I have a standard .NET windows service written in C#.

Can it install itself without using InstallUtil? Should I use the service installer class? How should I use it?

I want to be able to call the following: MyService.exe -install

and it will have the same effect as calling:

InstallUtil MyService.exe

c# .net deployment windows-services installutil

share|improve this question

asked Oct 31 '08 at 21:46

Yuval Peled
1,78241727

|

add comment (requires an account with 50 reputation)

5 Answers

active oldest votes

up vote 98 down vote accepted

Yes, that is fully possible (i.e. I do exactly this); you just need to reference the right dll (System.ServiceProcess.dll) and add an installer class...

Here's an example.

share|improve this answer

edited Oct 31 '08 at 21:56

answered Oct 31 '08 at 21:49

Marc Gravell♦
408k6210021524

8 |

+1d, see also stackoverflow.com/questions/1449994/… – Ruben Bartelink Jan 29 '10 at 9:22

@Adil works fine from here – Marc Gravell♦ Oct 12 '12 at 5:24

Its my fault, forgot that my kind proxy would be stopping me from https, I will delete my comments and request you to delete yours as they are not useful. – Adil Oct 12 '12 at 5:33

add comment (requires an account with 50 reputation)

up vote 19 down vote

Take a look at the InstallHelper method of the ManagedInstaller class. You can install a service using:

string[] args;
ManagedInstallerClass.InstallHelper(args);

This is exactly what InstallUtil does. The arguments are the same as for InstallUtil.

The benefits of this method are that it involves no messing in the registry, and it uses the same mechanism as InstallUtil.

share|improve this answer

answered Jun 18 '09 at 22:19

adrianbanks
29.5k461112

1 |

+1 for being very simple – romkyns Nov 28 '09 at 14:02

1 |

5 |

Why do you suggest this when the docs say this is not meant to be called by user code? – Leeks and Leaks Apr 15 '10 at 19:50

3 |

I use this method because 1. it is simple 2. it is exactly what InstallUtil does 3. you don't need to change the installer when on x86/x64 4. it hasn't changed in several versions of .Net - if it does, it will be picked up in testing and I will fix it then. – adrianbanks Jan 15 '11 at 23:48

add comment (requires an account with 50 reputation)

up vote 10 down vote

You can always fall back to the good old WinAPI calls, although the amount of work involved is non-trivial. There is no requirement that .NET services be installed via a .NET-aware mechanism.

To install:

Open the service manager via OpenSCManager.

Call CreateService to register the service.

Optionally call ChangeServiceConfig2 to set a description.

Close the service and service manager handles with CloseServiceHandle.

To uninstall:

Open the service manager via OpenSCManager.

Open the service using OpenService.

Delete the service by calling DeleteService on the handle returned by OpenService.

Close the service and service manager handles with CloseServiceHandle.

The main reason I prefer this over using the ServiceInstaller/ServiceProcessInstaller is that you can register the service with your own custom command line arguments. For example, you might register it as "MyApp.exe -service", then if the user runs your app without any arguments you could offer them a UI to install/remove the service.

Running Reflector on ServiceInstaller can fill in the details missing from this brief explanation.

P.S. Clearly this won't have "the same effect as calling: InstallUtil MyService.exe" - in particular, you won't be able to uninstall using InstallUtil. But it seems that perhaps this wasn't an actual stringent requirement for you.

share|improve this answer

edited Nov 30 '09 at 19:09

answered Nov 28 '09 at 13:39

romkyns
13.6k461139

|

add comment (requires an account with 50 reputation)

up vote 3 down vote

Here is a class I use when writing services. I usually have an interactive screen that comes up when the service is not called. From there I use the class as needed. It allows for multiple named instances on the same machine -hence the InstanceID field

Sample Call

IntegratedServiceInstaller Inst = new IntegratedServiceInstaller();
Inst.Install("MySvc", "My Sample Service", "Service that executes something",
_InstanceID,
// System.ServiceProcess.ServiceAccount.LocalService, // this is more secure, but only available in XP and above and WS-2003 and above
System.ServiceProcess.ServiceAccount.LocalSystem, // this is required for WS-2000
System.ServiceProcess.ServiceStartMode.Automatic);
if (controller == null)
{
controller = new System.ServiceProcess.ServiceController(String.Format("MySvc_{0}", _InstanceID), ".");
}
if (controller.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
Start_Stop.Text = "Stop Service";
Start_Stop_Debugging.Enabled = false;
}
else
{
Start_Stop.Text = "Start Service";
Start_Stop_Debugging.Enabled = true;
}

The class itself

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;

namespace MySvc
{
class IntegratedServiceInstaller
{
public void Install(String ServiceName, String DisplayName, String Description,
String InstanceID,
System.ServiceProcess.ServiceAccount Account,
System.ServiceProcess.ServiceStartMode StartMode)
{
//http://www.theblacksparrow.com/
System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
ProcessInstaller.Account = Account;

System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();

System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
string processPath = Process.GetCurrentProcess().MainModule.FileName;
if (processPath != null && processPath.Length > 0)
{
System.IO.FileInfo fi = new System.IO.FileInfo(processPath);

String path = String.Format("/assemblypath={0}", fi.FullName);
String[] cmdline = { path };
Context = new System.Configuration.Install.InstallContext("", cmdline);
}

SINST.Context = Context;
SINST.DisplayName = String.Format("{0} - {1}", DisplayName, InstanceID);
SINST.Description = String.Format("{0} - {1}", Description, InstanceID);
SINST.ServiceName = String.Format("{0}_{1}", ServiceName, InstanceID);
SINST.StartType = StartMode;
SINST.Parent = ProcessInstaller;

// http://bytes.com/forum/thread527221.html
SINST.ServicesDependedOn = new String[] { "Spooler", "Netlogon", "Netman" };

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

// http://www.dotnet247.com/247reference/msgs/43/219565.aspx
using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEM\CurrentControlSet\Services\{0}_{1}", ServiceName, InstanceID), true))
{
try
{
Object sValue = oKey.GetValue("ImagePath");
oKey.SetValue("ImagePath", sValue);
}
catch (Exception Ex)
{
System.Windows.Forms.MessageBox.Show(Ex.Message);
}
}

}
public void Uninstall(String ServiceName, String InstanceID)
{
//http://www.theblacksparrow.com/
System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();

System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext("c:\\install.log", null);
SINST.Context = Context;
SINST.ServiceName = String.Format("{0}_{1}", ServiceName, InstanceID);
SINST.Uninstall(null);
}
}
}

share|improve this answer

answered Oct 31 '08 at 22:08

Brad Bruce
3,60311130

|

add comment (requires an account with 50 reputation)

up vote 0 down vote

In the case of trying to install a command line application as a Windows service try the 'NSSM' utility. Related ServerFault details found here.

share|improve this answer

answered Oct 13 '11 at 16:35

John M
2,29343983

|

add comment (requires an account with 50 reputation)

Your Answer

draft saved

draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Stack Exchange

Post as a guest

Name

Email required, but not shown

discard
By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged c# .net deployment windows-services installutil or ask your own question.

tagged

c# × 492736
.net × 160710
deployment × 8883
windows-services × 4506
installutil × 67

asked

4 years ago

viewed

72402 times

active

1 year ago

Community Bulletin

blog

Podcast #50 – Listen To This Podcast

Linked

37
Inno Setup for Windows service?

21
Using InstallUtil and silently setting a windows service logon username/password

30
ClickOnce deploy a Windows Service?

6
How to write c# service that I can also run as a winforms program?

11
How to check if a windows service is installed in C#

3
What's the best way to create .NET Windows service installer?

2
What is the best way to install a C# windows service that doesn't require running installutil manually?

3
Problem with installing Windows service using AssemblyInstaller

2
Configuring the Package.xml for SQL Express ClickOnce Pre-Requisite install?

0
Is there an official GUI way of installing and removing .Net services on Windows Server 2008?

see more linked questions…

Related

25
Installing a .NET service using InstallUtil

1
Installing and running a .NET Windows service at a web host

2
Install windows service without InstallUtil.exe

0
Setting a window service to run as the current user when it is installed using InstallUtil.exe

19
Cannot install windows service

17
installing windows service with SC.exe or InstallUtil.exe - there is difference but which?

11
Using InstallUtil to install a Windows service with startup parameters

0
c# error on windows service

3
How to install a .NET windows service without InstallUtil.exe vb.net

4
Can I XCOPY Deploy a Windows Service?

question feed

lang-cs

about help badges blog chat data legal privacy policy jobs advertising info mobile contact us feedback

Technology | Life / Arts | Culture / Recreation | Science | Other

Stack Overflow

Server Fault

Super User

Web Applications

Ask Ubuntu

Webmasters

Game Development

TeX - LaTeX

Programmers

Unix & Linux

Ask Different (Apple)

WordPress Answers

Geographic Information Systems

Electrical Engineering

Android Enthusiasts

IT Security

Database Administrators

Drupal Answers

SharePoint

User Experience

Mathematica

more (13)

Photography

Science Fiction & Fantasy

Seasoned Advice (cooking)

Home Improvement

more (13)

English Language & Usage

Skeptics

Mi Yodeya (Judaism)

Travel

Christianity

Arqade (gaming)

Bicycles

Role-playing Games

more (21)

Mathematics

Cross Validated (stats)

Theoretical Computer Science

Physics

MathOverflow

more (7)

Stack Apps

Meta Stack Overflow

Area 51

Stack Overflow Careers

site design / logo © 2013 stack exchange inc; user contributions licensed under cc-wiki with attribution required

rev 2013.7.26.898