String Class vs. Microsoft.VisualBasic Namespace - Part IIString Class vs. Microsoft.VisualBasic Namespace - Part III

How To Convert a WinForms App To A Windows Service

August 14th, 2007

compassIn my previous article, How To Make Debugging .NET Windows Services Easier, I mentioned using a console or WinForms app to scaffold a Windows Service app for debugging and initial development. Unfortunately, there is a little ‘gotcha’ that needs to be addressed if you decide to do this.

When you’re ready to convert, you’ll want to set the startup object of your Windows Service to the name of your class that contains the service code, including the standard service Protected Overrides OnStart, OnStop, etc., and remove any test forms or Sub Main code from your project. What you will see in the My Project Application tab after doing this is that the old startup objects will remain on the list while your desired class isn’t on the list as shown here:

Service Startup Object Screen Capture

So, what can you do to correct this problem?

To handle this, you’ll need to edit a hidden generated Designer file because when you switch project types, certain code is not automatically generated. To get to the hidden file, bring up the Solution Explorer window and click on the ‘Show All Files’ button. It’s at the top and is the button circled in red below:

Solution Explorer Screen Capture

Next, you’ll want to expand the your service’s module to reveal the Designer module, as circled in blue above. Open this file and add the following code to it:

' The main entry point for the process
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()

    Dim ServicesToRun() As System.ServiceProcess.ServiceBase

    ' More than one NT Service may run within the same process. To add
    ' another service to this process, change the following line to
    ' create a second service object. For example,
    '
    '   ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
    '

    ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyService}

    System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End Sub

Of course, you would replace ‘MyService’ with the name of your service class. Now, when you go back to the My Project Application tab and click on the StartUp Object drop down you’ll see your service class on the list. Select it and you’ll be ready to compile your application as a Windows Service.

Please let me know if this article was helpful to you or if you have any other questions about this or other Windows Service issues by leaving me a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: Tip Sheets


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (3 votes, average: 4.33 out of 5)
Loading ... Loading ...

4 Comments Add your own

  • 1. Alyson  |  August 18th, 2007 at 8:11 pm

    hi nice post, i enjoyed it

  • 2. Aaron Throckmorton  |  September 19th, 2007 at 2:41 pm

    Fantastic article. This is exactly what I needed. Without this article I’d still be maintaining separate projects, and copying code back and forth. Thanks!

  • 3. jfrankcarr  |  September 19th, 2007 at 3:17 pm

    Thanks. I’m glad you found it helpful.

  • 4. laura  |  June 26th, 2008 at 9:55 am

    Thanks exactly wat i was lookin for

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives