Debug a service onDestroyService( )

If you simply put a breakpoint at the beginning of the DestroyService method and then try to quickly attach a debugger to your service instance process, you may miss the opportunity to debug the code you were hoping to debug. The DestroyService method may have already executed by the time you can attach your debugger to the service instance process. This is particularly likely if your workload completes very quickly, or if the service instance you want to debug starts up and shuts down very quickly if there is no workload to run.

To prevent the DestroyService logic from executing before you are ready to debug it, add an infinite while loop to the beginning of the DestroyService method. Make sure the infinite condition is not hard coded. You must be able to change the value of the variable to exit the while loop, so the execution flow can reach the true DestroyService logic.

  1. Modify your service to add the following loop to the beginning of your onDestroyService(…) method:
    // This is pseudo-code
    boolean stall = true;
    while (stall == true)
    {
    // Stall so machine is not bogged down by high CPU usage
    Sleep(1); 
    }
    // Your original onDestroyService(…) code
  2. Compile and deploy your modified service.
  3. Edit your application profile and change parameters as follows:
    1. Set the preStartApplication attribute to false.
    2. Set the taskLowWaterMark attribute to 1.0.
    <Consumer applicationName="MyApplication" consumerId="/consumer" taskHighWaterMark="1.0" taskLowWaterMark="1.0" preStartApplication="false" numOfSlotsForPreloadedServices="1"/>
  4. Use the soamreg command or the Console to update your application with your new application profile.

    Your update does not cause a session manager to be started because preStartApplication is set to false.

  5. Run the client application.

    This causes a session manager and at least one service instance to be started.

  6. Attach a debugger to the service instance:
    1. Your service instance is a running instance of the executable that was produced by compiling your custom implementation of ServiceContainer. Attach a debugger to your service instance.
    2. Add breakpoints that correspond to line 3 and line 8 in the pseudo-code above. When the debugger breaks at line 2, set the stall variable to false. This allows you to exit the infinite loop and step into your own code.
    3. Run your client application.
    4. Continue. Your debugger breaks at line 8 to debug your original onDestroyService(…) code.