473,543 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to write a self installing service in .NET

Self Installing Service

Enter a topic name to show or a new topic name to create; then press
Enter
..
Start by writing a service. This involves deriving a class from
System.ServiceP rocess.ServiceB ase and overriding a few methods. It's
detailed in the documentation.

Once you have your service written, you'll need to add an installer
class. This should derive from System.Configur ation.Install.I nstaller.
In this class's constructor, you need to add a
System.ServiceP rocess.ServiceP rocessInstaller to your Installers
collection. This will install the process your service lives in. Set
its Username and Password properties to match the username and
password of the account you'd like the process to run as.

You'll also need to add an installer for the service itself. The class
that does this is System.ServiceP rocess.ServiceI nstaller. The only
thing you need to do to it before adding it to the Installers
collection is to set the ServiceName property. IMPORTANT! This must
match the ServiceName you used in your ServiceBase-derived class.

The code that does all this looks like this:

using System;
using System.Configur ation.Install ;
using System.ServiceP rocess ;
using System.Componen tModel ;
public class MyInstaller : Installer
{
public MyInstaller ()
{
ServiceProcessI nstaller spi = new ServiceProcessI nstaller ();
spi.Username = "ISENGARD\\ CheckURL ";
spi.Password = "ihsxa9up";
ServiceInstalle r si = new ServiceInstalle r ();
si.ServiceName = " CheckURL ";
this .Installers.Add ( spi );
this .Installers.Add ( si );
}
}

If you've done installers before, you know this is the same code that
the wizard will write for you. The only difference is that InstallUtil
(the usual way of doing things) relies on the presence of the
[RunInstaller(tr ue)] attribute on the installer class. Adding won't
hurt, but we don't need it so I've left it out.

The next thing we need to do is get the classes we've inherited from
to do all the work. You can put this next bit of code anywhere you
like, but I sort of like the idea of my service being self-installing,
so I've added code to my Main method to look for the /install and
/uninstall command-line switches, and to act appropriately.

"Appropriat ely" in this case means instantiating an instance of the
TransactedInsta ller class, adding our installer to its Installers
collection, and setting a few things up. One of these is to tell the
installer the path to the assembly we're installing. We can retrieve
this using the Location property of the Assembly class. Oddly, the way
the installation stuff wants this information is for us to pass it in
an array of strings that contain what are essentially command line
parameters. My guess is that the installer classes were built to work
with InstallUtil in mind.

The other thing we need to do is pass in an empty Hashtable if we're
calling Install. The installer will use this to store some things
internally. We don't need to worry about that. When calling Uninstall,
we just pass null. Other than that, the code for both cases is pretty
similar.

Here's the code:

public class MyService : ServiceBase
{
// Service stuff omitted for brevity
static void Main( string [] args )
{
string opt = null ;
if ( args.Length > 1)
{
opt = args [0];
}
if (opt != null && opt.ToLower () == "/install")
{
TransactedInsta ller ti = new TransactedInsta ller ();
MyInstaller mi = new MyInstaller ();
ti.Installers.A dd (mi);
String path = String.Format ("/ assemblypath ={0}",
System.Reflecti on.Assembly.Get ExecutingAssemb ly ().Location);
String[] cmdline = {path};
InstallContext ctx = new InstallContext ("", cmdline );
ti.Context ( ctx );
ti.Install ( new Hashtable ());
}
else if (opt != null && opt.ToLower () == "/uninstall")
{
TransactedInsta ller ti = new TransactedInsta ller ();
MyInstaller mi = new MyInstaller ();
ti.Installers.A dd (mi);
String path = String.Format ("/ assemblypath ={0}",
System.Reflecti on.Assembly.Get ExecutingAssemb ly ().Location);
String[] cmdline = {path};
InstallContext ctx = new InstallContext ("", cmdline );
ti.Context ( ctx );
ti.Uninstall ( null );
}
}
}

I haven't talked about things like creating a log file or any of that
because I haven't figured it out yet! But this worked for me, and I
needed it, because InstallUtil doesn't work with services written in
Managed C++.
Nov 21 '05 #1
0 4829

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5696
by: Werner Merkl | last post by:
Hi, Python is really great, for small to big programs. For my colleagues and some circumstances I sometimes need to "compile" a script using py2exe. Cause I use Windows, I like to use the (Windows) ability, to add some version infos, comments, etc to the exe file. If I use explorer to check, these properties are visible and correct.
3
7934
by: Angelos Karantzalis | last post by:
Hi y'all, recently I've come across a situation where a web service needs to deal with an exception that might arise, originating from a COM+ component. It then returns an int value (please don't ask me why) indicating that there was something wrong with the requested operation. Assuming that I don't really know all the posible...
4
2046
by: Claire | last post by:
Hi, I know this isn't strictly C# language related, but my service IS written in c# and I checked out the list of microsoft newsgroups for win2000 and couldn't really see one that applies. I'm trying to test my service on a remote win2000 server machine, accessing the machine as user "administrator" on remote desktop. Im just at the install...
11
5676
by: tshad | last post by:
I have a small Windows Service program that just has a timer on it and it built fine. I try to install it using InstallUtil and I get the following message: System.ComponentModel.Win32Exception: The account name is invalid or does not exist, or the password is invalid for the account name specified I am in the same folder my program is...
2
1598
by: J | last post by:
hi, I'm having a problem installing a service created in vb.net 2003 on a windows 2000 server. I have installed the service on a few XP machines with out any problems...but the 2 servers (win 2000 server sp4) I have triend have the same error (both are on diffrent domains and have framework v1.1 installed; 1 is a domain controler, the other...
2
1904
by: Paulers | last post by:
Hello all, Can someone point me in the right direction to some information that will guide me in the creation of a self-installing service? I have a service that I created using vb.net and it works fine with installutil however, I would like to manipulate it from a form. for example, I would like to install, uninstall, start, and stop it...
0
1545
by: Waldy | last post by:
Hi there, I have written a C# service that self installs by running the ..exe with a parameter. A log is produced when it does this. At a customer site where they have had to re-install, they are getting an error. See the log file below. It fails because the name already exists, but then fails to uninstall because it does not exist! ...
3
1551
by: Mukesh | last post by:
Hi friends i wants to install a service (MS application Block) on the server using a web application. Can i do this if yes then how can i do this I m using MS VisualStudio.net 2003, C#, .net1.1 Mukesh Delhi India
2
2606
by: masterej | last post by:
Developers, I am unable to import the WMP control into a vs.net c# app. First, the WMP library does not appear in the COM objects list automatically. When I try to import wmp.dll from the system32 folder, a "self-registration failed" message appears. Has anyone seen this problem before? I am running VS.NET 2005 Pro (Service Pack 1) on...
0
7355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5889
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4899
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
979
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
648
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.