20.2 Global vs Local Scope SrvRoutines

Where you define your SrvRoutine can be quite important, specifically if you execute the same SrvRoutine multiple times asynchronously, that is you may execute the second call before the first call has completed.

For this reason, it is generally recommended to use Local Scoping for your SrvRoutine definitions.

Local Scope

In this example the SrvRoutine is defined within a method routine.

 

Mthroutine Name(Load)

* Define the service instance
Define_Com Class(#ServerModule.GetEmployees) Name(#GetEmployees)
  
* Execute asynchronously
#GetEmployees.ExecuteAsync( #List)

   Evtroutine Handling(#GetEmployees.completed)

    * Process complete

   Endroutine

 

   Evtroutine Handling(#GetEmployees.Failed)

    * Process failed

   Endroutine


Endroutine

Every time the method is called a new execution instance is created and thus by extension, a new instance of the SrvRoutine will also be created. The method routine, and any variables within it will stay in memory until the all processing has completed.

Global Scope

In this example the SrvRoutine is defined outside of a method.

 

* Define the service instance
Define_Com Class(#ServerModule.GetEmployees) Name(#GetEmployees)

Mthroutine Name(Load)
   * Execute asynchronously
   #GetEmployees.ExecuteAsync( #List)
Endroutine

Evtroutine Handling(#GetEmployees.completed)

   * Process complete

Endroutine

 

Evtroutine Handling(#GetEmployees.Failed)

   * Process failed

Endroutine

 

This means that there will only ever be one instance of the routine. Multiple requests to execute the routine, perhaps with different parameter values, will result in multiple submissions to the web server and consequently, multiple Completed events will fire. However, as there is only one instance of the routine it is impossible to distinguish between the many executions.