DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Debugging a Windows Service Project

Anders Abel user avatar by
Anders Abel
·
May. 30, 12 · · Interview
Like (0)
Save
Tweet
5.89K Views

Join the DZone community and get the full member experience.

Join For Free

a service project created in visual studio cannot be directly started in the debugger. a small tweak enables direct running of the service and gets rid of the need to deploy the service to test it.

from the first time i tried developing a windows service in c++ back in the good old days before .net i’ve always found them awkward to debug. when developing i want to have my environment setup with multiple startup projects so that i can open a solution, hit f5 and have everything required running. if the solution contains a service a tedious compile-install-start service-attach debugger procedure is required.

last week when i created a service i decided to find a better way, a way that enables the simple “press f5 to get everything running” approach. since a service project already compiles to an .exe file, it can be changed to check if it is run as a service or interactively during startup. if running interactively, a reflection hack is used to call each service’s onstart method.

the first step is to change the service application to be a console app instead of a windows app. i you prefer to use winforms or wpf for the interactive gui you should leave it as it is, but that would make a background service dependent on ui libraries which i don’t like. besides, the gui is not the essential part of this app. it’s target at developers so a console app will do.

first the main function has to be modified to check if it is run interactively or as a service.

static void main()
{
    servicebase[] servicestorun;
    servicestorun = new servicebase[] 
    {
        new myservice()
    };
    if (environment.userinteractive)
    {
        runinteractive(servicestorun);
    }
    else
    {
        servicebase.run(servicestorun);
    }
}

a check is done for environment.userinteractive to choose between interactive mode and normal service mode. this might cause problems with services running in interactive mode. (i haven’t tested and don’t intend to – because a service running interactively is often a security problem. if you have to communicate with the user, build a small client app that is auto started instead.)

the real work is done in the runinteractive helper method. it calls the onstart method of each service. then it waits for a keypress before calling onstop of each service. the onstart and onstop methods are protected and not intended to be called from the outside. since this is a debug aid i think it is okay to use reflection to call the nonpublic methods.

static void runinteractive(servicebase[] servicestorun)
{
    console.writeline("services running in interactive mode.");
    console.writeline();
 
    methodinfo onstartmethod = typeof(servicebase).getmethod("onstart", 
        bindingflags.instance | bindingflags.nonpublic);
    foreach (servicebase service in servicestorun)
    {
        console.write("starting {0}...", service.servicename);
        onstartmethod.invoke(service, new object[] { new string[] { } });
        console.write("started");
    }
 
    console.writeline();
    console.writeline();
    console.writeline(
        "press any key to stop the services and end the process...");
    console.readkey();
    console.writeline();
 
    methodinfo onstopmethod = typeof(servicebase).getmethod("onstop", 
        bindingflags.instance | bindingflags.nonpublic);
    foreach (servicebase service in servicestorun)
    {
        console.write("stopping {0}...", service.servicename);
        onstopmethod.invoke(service, null);
        console.writeline("stopped");
    }
 
    console.writeline("all services stopped.");
    // keep the console alive for a second to allow the user to see the message.
    thread.sleep(1000);
}

with the service library set up this way it can be set to start together with other projects automatically when starting a debug session in visual studio. enabling everything to start with one button (f5) really speeds up the coding and debugging.

Published at DZone with permission of Anders Abel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Books Every Senior Engineer Should Read
  • Querying Kafka Topics Using Presto
  • Maven Tutorial: Nice and Easy [Video]
  • Debugging Deadlocks and Race Conditions

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo