Debug a service onCreateService( )

If you simply put a breakpoint at the beginning of the CreateService method and then try to quickly attach a debugger to your service instance process, you will likely miss the opportunity to debug the code you were hoping to debug— the service instance manager initiates the invocation of the CreateService method almost immediately after it starts the service instance process, and the CreateService method may already execute before you attach your debugger to the service instance process.

To prevent the CreateService logic from executing before you are ready to debug it, add an infinite while loop to the beginning of the CreateService 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 CreateService logic.

  1. Modify your service to add the following loop to the beginning of your onCreateService(…) 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 onCreateService(…) code
  2. Compile and deploy your modified service.
  3. Edit your application profile and change parameters as follows:
    1. Set the preStartApplication attribute to true.
    2. Set the numOfSlotsForPreloadedServices attribute to 1.
    <Consumer applicationName="MyApplication" consumerId="/consumer" taskHighWaterMark="1.0" taskLowWaterMark="0.0" preStartApplication="true" numOfSlotsForPreloadedServices="1" />
  4. Use the soamreg command or the Console to update your application with your new application profile.

    Your update causes the session director to immediately start a session manager to service your application (preStartApplication="true").

    Your application's session manager preloads a service instance even before you send it any workload (numOfSlotsForPreloadedServices="1"). However, because of the loop introduced in step 1, your onCreateService(…) method does not complete.

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