• Home >>  Java >>  Programming Tips
  • debugging windows service without deploying it


  • Rating : Excellent[0]   Very Good[0]   Average[0]   Below Average[0]   Poor[0]
  • Pub Date: 10/14/2008
  • admin [ View Profile ]

  • Abstract
  • At the beginning, there was no easy way to build a Windows service using Visual Studio and .NET Framework. Now that Microsoft has integrated this functionality as a template and a class library, it's much more easy to do that. This is good, but to test your newly created service you have to deploy it. That means you must add an installer and build a whole MSI file and run it in your environment. Once deployed, you can start it with Wind......

  • Introduction
  •  Sponsored Links
  • screenshot - easydebug<b>windows</b><b>service</b>s.jpg

    introduction

    at the beginning, there was no easy way to build a windows service using visual studio and .net framework. now that microsoft has integrated this functionality as a template and a class library, it's much more easy to do that. this is good, but to test your newly created service you have to deploy it. that means you must add an installer and build a whole msi file and run it in your environment. once deployed, you can start it with windows service controller and attach a debugger to it.

    i don't know how this sounds to you, but to me it is too much unneeded work to do a quick test. this article describes a much more easy way to do that.

    using the code

    you will find a sample application using this concept. here is how it works.

    first you need a working windows service. (for more information, refer to this.)

    in visual studio, building a web service is easy. create a new project and select windows service template.

    then add servicedebuggerhelper project to your solution and a reference to it. you can find it in the sample code.

    by default, when you create a windows service project, visual studio will create two classes: a program class and a service1 class. here is what the program class looks like:

    static class program 
    { 
        /// <summary>
        /// the main entry point for the application. 
        /// </summary>
        static void main() 
        { 
            servicebase[] servicestorun = new servicebase[] { new service1() }; 
            servicebase.run(servicestorun); 
        } 
    }

    the first thing we have to do is to surround that code with a conditional statement to let us control if we want to run as a real service or just in debugging mode.

     static class program 
    { 
        /// <summary>
        /// the main entry point for the application. 
        /// </summary>
        static void main() 
        { 
            if (args.length > 0 && args[0].tolower().equals("/debug")) 
            { 
                application.run(new servicerunner(new service1())); 
            } 
            else 
            {        
                servicebase[] servicestorun = new servicebase[] { new service1() }; 
                servicebase.run(servicestorun); 
            }
        } 
    }

    in this new program class, servicerunner will be acting as a service host. to make it work, you have to add the "/debug" command line option in your project properties.

    go to project -> ... properties -> debug and type /debug in the command line arguments field.

    from there, you have two options to make your service debuggable. either you implement idebuggableservice or inherit from debuggableservice base class.

    implementing idebuggableservice interface

    if you already subclass servicebase and use that subclass for all your services or if you just want to have full control over the implementation, you should implement the idebuggableservice interface. here is its definition:

    public interface idebuggableservice
    {
        void start(string[] args);
        void stopservice();
        void pause();
        void continue();
    }

    notice that the stop method is called stopservice to avoid conflict with the existing stop method of the servicebase class.

    here is a sample implementation of this interface:

    public partial class service1 : servicebase, idebuggableservice        
    {
        //
        // your existing service code
        //
        // ...
        //
        
        #region idebuggableservice members
    
        public void start(string[] args)
        {
            onstart(args);
        }
    
        public void stopservice()
        {
            onstop();
        }
    
        public void pause()
        {
            onpause();
        }
    
        public void continue()
        {
            oncontinue();
        }
    
        #endregion
    }

    subclassing debuggableservice

    subclassing your service from debuggableservice is even more simple. all you have to do is change the base class from servicebase to debuggableservice.

    public partial class service1 : debuggableservice        
    {
        //
        // your existing service code
        //
        // ...
        //
    }

    all the control methods are already implemented by debuggableservice.

    points of interest

    in my sample code, i added a windows form class to let you start, stop and pause your service. you can use it as is, build your own controller interface or just call the methods in your program class. it is up to you.

    of course you should still deploy your service to give it the final test run. one good reason to do this is to test the actual security context in which your service will run. it will not have the same permission as when you run it in visual studio, but this technique can speed up the development process.

  • RATE THIS ARTICLE :
  •  
    • Latest Comments:
      • Add a comment
      • Title:
      • Comment
      •  
    Other popular Programming Tips articles:
    • VSS: protocol handler for Visual SourceSafe

      Imagine that you could open a document in a Visual SourceSafe database with one mouse click.

    • How to use google and other tips for finding programming help

      Code Project is a wonderful resource. Certainly, my career as a developer is built on the help I got here learning MFC in late 1999. However, it's increasingly the case that a lot of the questions on our forums are easily answered by a simple google search, so I'm writing this article to explain how

    • broadcast a message to multiple instances of an application

      This application describes how to broadcast a message to multiple instance of an application using SendMessageNotify API. DetailsFollow the following steps to broadcast message :-Register the message function and get its id using RegisterWindowMessageAPI. This function will return a unique id for th

    • about rss

      Want more traffic? Searching for an easy way to distribute your news? You need an RSS news feed. Thousands of web sites today use RSS as a "what's new" mechanism to drive traffic their way. RSS provides an easy way to monitor fresh content. RSS feeds highlight new material so that you don'

    • Developing applications that always decease gracefully.

      It is always a dream for every programmer under the sun, to write programs that never smash. So this article presents some guidelines in trapping exceptions which can kill applications, and thereby ensuring the graceful exit of a program. I have included some code snippets that show how to trap exce

    • unit test utility: restoring database state after each unit test execution

      Need: Lets start with an example, suppose we are writing unit tests for following two cases: GetCustomersByIdGreaterThan() AddCustomer() Initial state of database is like We run first unit test case for GetCustomersByIdGreaterThan()[Test]public void CanGetCustomersByIdGreaterThan(){const int CUSTOME

    • complexconverter - make configuration still more flexible

      Normal|0; 0; 765; 321|2|Knoten0|2|Knoten1|0|False|Knoten2|3|Knoten4|0|False|Knoten5|0|False|Knoten6|0|False|False|True|Knoten7|2|Knoten8|0|False|Knoten9|0|False|False|90|90|90|90|2|4|01|02|03|04|4|11|12|13|14Inside ComplexConverterComplexConverter has a problem, when set up, or conversion-code is ch

    • driving or automating gui applications

      Sometimes you want or need to control (or automate the use of) an application but the application provides no automation API (command line interface or CLI, COM component, .NET API DLL, web service, etc.), so what do you do? You're left with (graphical) user interface (GUI) automation. Although, in

    • unit testing starter - vs.net 2008

      Unit Testing starter - VS.NET 2008 Hi Everybody,As you know, VS.NET 2008 comes with unit testing integrated within. To start with a testdriven approach, we need to understand how unit testing works in VS.NET 2008. Many of usmight be using already available tools / frameworks for this, viz - NUnit (M

    • cost reduced swap

      void main() { int i=10,j=20; j=i+j-(i=j); } Here the two variables are swapped in a single statement . The cost is reduced than the known swapping methods. Temporary variables are not used.addition and subtraction only.can be used for any data types and this is efficient than the normal methods.time

    About Us |Contact us |Site Map |Csharp |Visual C / C++ |Visual basic |Java |SQL |Linux / Unix |Ajax
    ©2007-2018 CodeCoolest.com. Ptolive.cn Asp.net source code All Rights Reserved.