Windows Services in C#: Adding the Installer (part 3)

May 23, 2007 — arcanecode

OK, you’ve crafted your service, now you’re ready to install it so you can test. To do so you’ll need to create an installer for your project. However, you don’t create an installer in the traditional fashion.

Instead, switch to the “TimeLoggerService.cs [Design]” tab. Now in that big gray area right click, and pick “Add Installer”.

Visual Studio will do some magic and you’ll have a new ProjectInstaller.cs added to your project. It also added a few new references to the solution. If the “ProjectInstaller.cs [Design]” tab is not up, bring it up, and click on the serviceInstaller1 item.

Let’s start by giving it a decent name, I chose ArcaneTimeLoggerServiceInstaller. Now for the Description property I entered “The Arcane Code Time Logging Service”. For DisplayName I gave it “Arcane Code Time Logger”. Finally, I’m leaving the StartType property to Manual, you may wish to alter this for your “real world” service.

Now go back and click on the serviceProcessInstaller1. We’ll change it’s name to ArcaneTimeLoggerServiceProcessInstaller. If you remember the discussion from part 1, you will recall a discussion about the security. Here in the Account property is where you will want to set that. Since all this sample does is a minimal amount of logging, I can go with a fairly low level of security and set to “LocalSystem”.

OK, we’re almost done. Right click the project name (in my case TimeLogger) and select properties from the menu. (Note, make sure to click the project, not the solution!) Now on the Application tab, under “Startup object” pick TimeLogger.Program. Now save everything and build your project.

Assuming your build was successful, you can now install and test your windows service. There are two ways to install, we can use the installutil.exe, or create a full blown MSI installer. Since we are just at the point of debugging, we will use the simple installutil.exe.

To preset all the pathing you’ll need for install util, we’ll need to open a Visual Studio Command Window. Start, All Programs, Microsoft Visual Studio 2005, Visual Studio Tools, Visual Studio Command Window. If you are running under Vista, STOP! Do NOT click on Visual Studio Command Window. Instead, right click and pick “Run as Administrator”. Again that’s for Vista, for XP just click since you likely have Admin rights.

The moral is without Admin rights InstallUtil fails every time, and it drove me up the wall trying to figure this out.

Now in the command window navigate to the bin\debug folder where your project compiled. Type in installutil TimeLogger.exe (or whatever you named your exe).

If everything goes well, you s hould get the messages “The Commit phase completed successfully” and “The transacted install has completed”. Now let’s go see if we were successful.

Open the Microsoft Management Console (Start, Run, MMC)). When it opens, pick the Services and EventViewer snap-ins. Under Services, you should easily find the ArcaneTimeLogger, just double click on it and start it. Once it starts you can close the dialog.

Now head over to the Event Viewer. Click on the “Create Custom View”, to make it easy to find our log events. In the “Create Custom View” dialog, select “By source” and in the drop down check the ones for ArcaneTimeLogging. Click OK to close.

Your view should now update to look something like this:

Congratulations, you’ve now coded and installed a basic windows service, and more over logged events from your service. This sample app we just created can serve as a basic template for all of your future windows services.

By the way, we should probably not get carried away with the euporia. Let’s take a moment and clean up. Return to the services area of the MMC and double click on our ArcaneTimeLogger. Now Stop the service, so it won’t be continually logging the time.

Now that it’s not running, let’s uninstall it. Return to the Visual Studio Command Window and simply type the command “installutil /u TimeLogger.exe”. The /u switch will tell InstallUtil to uninstall our service named TimeLogger.exe. And with that you’ve take care of your clean up work. Tomorrow we’ll talk a bit about debugging a windows service.

Like this:

Related

I kept getting an error “The source ‘Service1’ is not registered in log ‘Service1Log’. (It is registered in log ‘Application’.) ” The Source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property.” When i tried to run this service so i had to do this when creating the event log:

I tried the example above and it ran… but the log entries I could see were the start / stop service log entries. I attached the debugger and I can see the handler for the Elapsed event being called, and no uncaught exceptions are reported by the debugger. The EventLog.WriteEntry() function does not return an error code, so I cannot figure out why this does not work.

Figured it out. EventLog.WriteEntry() fails if there are spaces in the source string. A bit strange that no exception is thrown when such a failure occurs–makes it a little tricky to debug as the service just keeps moving along despite the failure to log the entry.

You can go one step farther if you name your service the same as the project name. Ex.
PRE-BUILD:
net stop “$(TargetName)”
%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u “$(TargetName)”
POST-BUILD:
%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i “$(TargetName)”
net start “$(TargetName)”

Hi all,
Has anyone already tried calling statich methods within the windows service?? it seems that i’m facing some problems with this…
Sorry if’t not the appropriate page to post this question.
Regards.

hi, I tried the obove example, but when I tried to install it using installutill, I get the message:
‘istallutil’ is not recognized as internal or external command, operable program or batch file
I’m using a computer in a network, i asked the network administrator to put me as administrator because it is required as i read obove, he said i’m already administrator.

can someone help me why i get this message, althought I am administrator?

hi, i found the solution and i wan to share it
for the command window to recognize installutill.exe, i have to put the whole path:
for VS 2005:
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe C:\MyService.exe

for VS 2003:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe C:\MyService.exe
ofcourse there is space between the installutil path and the win service paht, but if you’re already in the folder of the win service you don’t have to write its path

I have a problem with my windows services is: Right now, i using the Local System account for my service, the service have to download and save the files to a remote PC. but the services can’t save the files to the remote PC because it is missing permission. i have received a message in event log “logon fail, bad usernam or password”… so, how to pass this problem? any solution for me?

Hi. I just about going nuts here. I’ve created my service, added the ProjectInstaller, set all the properties, Added the Installation Project, added my service as the project output, and deployed. The setup program runs fine, the project is created on the specified location, BUT THE SERVICE IS NOT CREATED!!! What am I missing?

I too have the same problem as Hanno. I am using VS2008. I have triple checked everything and still cannot get the service to install on my W2K3 server. My build platform is Vista. Can anyone shed some light?

Just to throw my hat in the ring, I am having the same problem with Vista and VS2008… I am using a setup project and when I install nothing shows anywhere …not the services control app nor the log except to say that it was installed. I hope someone can shed some light.

Hi Otis,
You have to add a custom action in your MSI project.
So in custom actions, add the output of your service to the Install section. This will then run the required installation code for your service. i.e. the same as what the installutil.exe does
Hope that helps
Stewart

I’ve went thru this whole thing twice. It compiles and installs with no errors. I see one entry in the event log and that’s it. I refresh after a minute or 2 goes by…still nothing. I am using the Event viewer. This is where it should be showing up, right?

The real world problem is that InstallUtil.exe will not be available on the user’s machine. You can use sc.exe that is available on any Windows machine to install your Win service. The problem is that you’ll have to explain to the end user how to use sc.exe. It’s pretty much impossible. If you need to distribute your service to a number of users try some online installer builders like http://installer.codeeffects.com to build your installer. Google for others. there are plenty of such services out there.

I have a windows service developed in C#. My target environment is Windows 2008 Server. I install the service using installutil from cmd prompt runas administrator.

The problem scenario is as follows:
I have a batch file which will start the service. The batch file cannot be runas administrator. How can I start the service without running the batch file as “runas” administrator

I am no longer sure where you’re getting your info, however good topic. I needs to spend some time studying more or figuring out more. Thanks for great info I was in search of this information for my mission.

I do following your steps, and it works fine. But when I open MMC window and open the service which I have made, I do not know where it is, And the extension of the service is ArcaneTimeLogger,msc?
I get stucked with this step
“Open the Microsoft Management Console (Start, Run, MMC)). When it opens, pick the Services and EventViewer snap-ins. Under Services, you should easily find the ArcaneTimeLogger, just double click on it and start it. Once it starts you can close the dialog.” Would you please display the pictures for this step?

By the way, Thank you very much for this post! I am very very new to Window service but this post is a guide to help me for my work!

OK, we’re almost done. Right click the project name (in my case TimeLogger) and select properties from the menu. (Note, make sure to click the project, not the solution!) Now on the Application tab, under “Startup object” pick TimeLogger.Program. Now save everything and build your project.

What is TimeLogger.Program? There is absoluteky no explanation in your writeup about this. it just pops out of the blue. Help!!!!!!!!!!!!!!!!!!!!!!!!!!!