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

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”.

[Picture of Add Installer Menu]

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).

[Picture of Command Prompt]

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.

[Picture of Create Custom View]

Your view should now update to look something like this:

[Picture of MMC with our Events]

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.

About these ads

59 Responses to “Windows Services in C#: Adding the Installer (part 3)”

  1. tittatty Says:

    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:

    EventLog evt = new EventLog(“Application”, System.Environment.MachineName, “Service1″);

  2. Silvio L. Says:

    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.

    Any ideas?

  3. Silvio L. Says:

    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.

  4. manasi bopardikar Says:

    I did not see the service in the MMC

  5. Mark Hubers Says:

    You can use the pre and post build options to manage loading and unloading service. Ex,
    PRE-BUILD:
    net stop “ServiceName”
    %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u “ServiceName”
    POST-BUILD:
    %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i “ServiceName”
    net start “ServiceName”

    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)”

  6. Yuriy Says:

    To make last code work should add “.exe”
    EX:
    net stop “$(TargetName)”
    %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u “$(TargetName)”.exe
    %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i “$(TargetName)”.exe
    net start “$(TargetName)”

  7. sami Says:

    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.

  8. Pavan Says:

    Is “Create Custom View” for Event Logger only available in Vista. I could not find it XP.

  9. Mr me Says:

    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?

  10. Mr me Says:

    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

  11. Nguyen Trong Thanh Says:

    Dear arcanecode,

    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?

  12. oblade Says:

    Hey Mr me. I think you wanted to say this:

    for VS 2003:
    C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\InstallUtil.exe C:\MyService.exe

    for VS 2005:
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe C:\MyService.exe

  13. Hanno Says:

    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?

  14. webequipped Says:

    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?

  15. Tom V Says:

    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.

  16. Otis Says:

    I am having issue as Tom V and Hanno, working in Vista

    • Stewart Simpson Says:

      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

      • m3nta7 Says:

        It worked after I added a custom action. Thanks! It wasn’t obvious to me where the “Add Custom Action” menu was, so if anyone else needs to know where it is:

        Select the Custom Action Installer project in Solution Explorer. On the View menu, point to Editor, and then click Custom Actions.

  17. Stephen Henry Says:

    It worked absolutely fine at the first go itself :-) Thanks a lot

  18. Irfan Cehajic Says:

    To repeat Warrick Wilson’s comment here, as I encountered the same problem he had (his comment was on day 2 page).

    “I’m using VS 2008 and couldn’t get the timer event to log anything. After a little Googling and trying a few things, I got the following to work:

    protected void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
    string sSource = “ArcaneTimeLogger”;
    string sLog = “Application”;

    if (!EventLog.SourceExists(sSource))
    EventLog.CreateEventSource(sSource, sLog);

    EventLog evt = new EventLog();
    string message = “Arcane Time:”
    + DateTime.Now.ToShortDateString() + ” ”
    + DateTime.Now.ToShortTimeString();
    evt.Source = “ArcaneTimeLogger”;
    evt.WriteEntry(message, EventLogEntryType.Information);
    }

  19. anto Says:

    hi…i want to ask something…
    im using vs2008…
    why my windows service suddenly stopped without information in the viewer log???
    and its appear vs2008 debuger option.

    is there someone can help me???
    thanx…

  20. noam Says:

    just want to say thanks

  21. Sane Pete Says:

    Problem:
    System.ComponentModel.Win32Exception: No mapping between account names and security IDs was done

    Solution:
    Go to InitializeComponent() in ProjectInstaller.cs
    Right Click and choose ‘Go To Definition’.
    Add this to the InitializeComponent() function:

    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

    It all works lovely.

  22. Paresh Says:

    Hi, thanks for sharing this.
    I am having one problem. I am using VB 2005 Express Edition and in that I don’t have “Add Installer” Option in the menu so how do add installer Programmatically??

    Please reply to me – it is very argent for me.. thanks!

    Regards,
    Paresh

  23. Paul Says:

    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?

  24. Web Hunter Says:

    Hi,

    I wrote a windows service and it was installed successfully.

    But when I am trying to start it, it is giving me an error.

    “Windows could not start the service on Local Computer.
    Error 1053: The service did not respond to the start or control request in a timely fashion.”

    I googled a lot for this. I found few solutions of .net framework 1.1 SP1, clearing logs etc.

    I already have .net framework 1.1 SP1, 2.0, 3.0, 3.5 SP1. I also tried clearing logs. But didn’t get success.

    Could you please help me out?

    Btw, the service is working on other system of my team mates. We all have same configuration.

    Don’t know the problem.

  25. Animesh Says:

    Hi Robert,

    I was able to get it right the first time. Your tutorial is really a great insight into the services world.

    Thank you.

  26. Алексей Иванов Says:

    Блин… Действительно красиво написано! Все это так знакомо…и правдиво!

  27. Lesha Says:

    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.

  28. Nisha Says:

    Hi,
    Great article. Thanks a lot.This article is helpful

  29. how to remove spyware Says:

    Hi from Paris! May i quote a post in your blog using the link to you? I’ve tried contacting you about this issue but it appears i cant achieve you, please response when get a moment, thanks.

  30. Jithendra Says:

    How to create a full blown MSI installer for Windows Service ?

  31. JJ Says:

    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

  32. Сергей Селихов Says:

    Как сделать идеальный минет – .

  33. Aquafan Says:

    Добрый день. Предлагаем продажу, установку и оформление аквариумов в СПб. Профессиональный подход, гарантии на все наши аквариумы 3 года, собственное производство.
    Подробности и фото наших прекрасных работ на нашем сайте, будем рады, если загляните =).обслуживание аквариумов спб
    Спасибо
    Акватэк
    ВНИМАНИЕ !!! Cкоро открытие гипермаркета аквариумистики. 350м2, более 5000 наименований товара
    тел.: +7 (812) 369-40-09, 740-13-45;
    Санкт-Петербург ул. Варшавская д. 23 к. 1
    Режим работы с 10 до 20 часов, без обеда и выходных

  34. Multsbornik.ru Says:

    Много разных мультфильмов. Мультфильмы СССР.

  35. supergeymer Says:

    На ресурсе можно скачать игры, а также посмотреть или разместить информацию о них. Ресурс посвящён исключительно компьютерным развлечениям.

  36. http://informs.ucoz.com Says:

    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.

  37. Подать объявление Says:

    Wow, superb weblog structure! How lengthy have you ever been blogging for? you make blogging look easy. The overall look of your web site is excellent, as smartly as the content material!

  38. computer mouses Says:

    Good morning! Ive just returned from France, there were billboards everywhere jeux concours gratuits, just what does it mean? Thanks!

  39. wannaliza Says:

    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!

    Suwanna

  40. Saquib Says:

    Can we add a drop down list to the installer window for choosing the database and dsn names

  41. Shams Says:

    Thank you – This post was helpful to me

  42. Simple Installer for Windows Service using Visual Studio 2012 « candor developer Says:

    [...] you have the tooling in place, then just follow the instructions at arcane code for using the designer surface to rename a few items and add a description.  Or follow along here [...]

  43. Nissim Says:

    I got stuck when trying to follow these instructions:

    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!!!!!!!!!!!!!!!!!!!!!!!!!!!

  44. Guru jazzmatazz Says:

    What’s up to every single one, it’s truly a
    nice for me to pay a quick visit this web site, it includes important Information.

  45. car crash video Says:

    Hi friends, good piece of writing and nice arguments commented at this
    place, I am in fact enjoying by these.

  46. Raul Says:

    I really likje what you gus are սsually սp tߋo.

    Ƭhis type of clever ѡork annd coverage! Keeep սp thе superb աorks
    guys І’ve included yoս guys tо my blogroll.

  47. valentines day quote Says:

    When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time
    a comment is added I get four emails with the same comment.
    Is there any way you can remove me from that service?
    Thanks a lot!

  48. https://play.google.com/ Says:

    Howdy! Do you know if they make any plugins to assist with
    Search Engine Optimization? I’m trying to get my blog to
    rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Thank you!

  49. sniper elite III Says:

    Hi would you mind stating which blog platform you’re using?
    I’m going to start my own blog soon but I’m having a tough time
    selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and
    I’m looking for something unique. P.S Sorry for getting off-topic but
    I had to ask!

  50. cms property Says:

    Hi colleagues, its impressive article regarding teachingand completely defined, keep it up all the time.

  51. Nos pires voisins gratuit Says:

    Reality based films also grab the attention of many.
    He, along with the other townspeople, watches the beautiful theater being demolished.
    Hence, the people are well educated and highly concerned about the development of the region and keenly look forward to make their lives always easy.

  52. ditzcuhoumi Says:

    http://goo.gl/GVNMgr Заказать отличный виноград персик недорого
    телефон: +380977979464, +380660806387, +79788591825, +380656434040

    Правильнее звонить или на бытовой телефон, или на +7, т.к. есть возможность выключенья МТС.

    Продаю виноград с поля урожая 2014 года – средний и крупный опт. Сорта: Аркадия, Лора, Кодрянка, Кишмиш.Крым, Джанкойский район .

    Продаю самый лучший виноград персик со скидкой, Продаю неповторимый виноград персик недорого, Продам лучший виноград кишмиш недорого,

  53. psn karte Says:

    Great web site you have got here.. It’s hard to find quality writing
    like yours these days. I truly appreciate
    people like you! Take care!!

  54. http://twitch-league-of-legends.tumblr.com/ Says:

    Context: The previous version of speed shrine buff allowed for
    people ganking bot lane to come in at basically Ghost movement
    speeds, with the gankee having very little reaction time on a wardless map.
    Knowing when to move up and pressure and when to play defensive while free
    farming is the characteristic of not only a great ADC but of any good laner in League of Legends.
    The premise of the game is to control one of many champions and fight
    your way past hordes of spawning troops (called creep), then advance past towers to destroy the enemy base.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 100 other followers

%d bloggers like this: