Skip to content

Tutorial 04 06 Add Main Dispatcher Class

Matt Linder edited this page Jun 30, 2023 · 15 revisions

Harmony Core Logo

Tutorial 4: Adding the Main Dispatcher Class

In addition to a routine dispatcher class for each traditional Synergy routine you want to expose, your Traditional Bridge implementation must have a master dispatcher class that registers the individual routine dispatchers with the Traditional Bridge library code, enabling dispatching to these routines as needed.

When we generated code after adding an interface from our Synergy method catalog, a master dispatcher class was created for us. (We'll soon add this class to the solution.) But if you create a Traditional Bridge environment from scratch, you will need to create the master dispatcher. See Generic Code for the Master Dispatcher below, and note that we recommend placing the master dispatcher class in the same folder and namespace as your routine dispatcher classes.

Generic Code for the Master Dispatcher

The following is generic code for the master dispatcher class. Like the routine dispatchers, the class inherits from RoutineDispatcher. Also notice that there is a method for the interface, and for each routine there is a statement that adds the routine's dispatcher to the mDispatchStubs dictionary. This code includes <interface_name> and <routine_name> placeholders, which represent the name of the interface and the name of a traditional Synergy routine:

;;*****************************************************************************
;;
;; Title:       <interface_name>Dispatcher.dbl
;;
;; Description: Declares dispatcher classes for exposed methods
;;
;;*****************************************************************************
;; WARNING: GENERATED CODE!
;; This file was generated by CodeGen. Avoid editing the file if possible.
;; Any changes you make will be lost if the file is re-generated.
;;*****************************************************************************

import Harmony.TraditionalBridge
import TraditionalBridge.Dispatchers.<interface_name>

namespace TraditionalBridge.Dispatchers

    public partial class <interface_name>Dispatcher extends RoutineDispatcher

        public method <interface_name>Dispatcher
        proc
            ;;Declare dispatcher classes for the '<interface_name>' interface methods
            mDispatchStubs.Add("<routine_name>", new <routine_name>_Dispatcher())
            InitHook()
        endmethod
    endclass

endnamespace

Notice that the dispatcher method ends with a call to InitHook(), which is an empty method in the inherited RoutineDispatcher class. Traditional Synergy does not support partial methods, so this call emulates partial method functionality. It enables you to add logic to the dispatcher method without the risk of losing that code when code is regenerated.

Add the Master Dispatcher

  1. In Solution Explorer, right-click the Dispatchers folder (TraditionalBridge\Sources\Dispatchers) and select Add > Existing Item from the context menu.

  2. In the Add Existing Item dialog, navigate to the Dispatchers folder, select BridgeMethodsDispatcher.dbl, and then click Add.

  3. Open the BridgeMethodsDispatcher.dbl file, which should look like this:

;;*****************************************************************************
;;
;; Title:       <interface_name>Dispatcher.dbl
;;
;; Description: Declares dispatcher classes for exposed methods
;;
;;*****************************************************************************
;; WARNING: GENERATED CODE!
;; This file was generated by CodeGen. Avoid editing the file if possible.
;; Any changes you make will be lost if the file is re-generated.
;;*****************************************************************************

import Harmony.TraditionalBridge
import TraditionalBridge.Dispatchers.<interface_name>

namespace TraditionalBridge.Dispatchers

    public partial class <interface_name>Dispatcher extends RoutineDispatcher

        public method <interface_name>Dispatcher
        proc
            ;;Declare dispatcher classes for the '<interface_name>' interface methods
            mDispatchStubs.Add("AddTwoNumbers", new AddTwoNumbers_Dispatcher())
            mDispatchStubs.Add("GetEnvironment", new GetEnvironment_Dispatcher())
            mDispatchStubs.Add("GetLogicalName", new GetLogicalName_Dispatcher())
            InitHook()
        endmethod
    endclass

endnamespace

Update the Generated Code

Now we'll use the Harmony Core Upgrade Tool to update all generated code (along with other Traditional Bridge components, as discussed in Updating Traditional Bridge):

  1. In Visual Studio, select Tools > Command Prompt (x64) to open a command prompt window.

  2. In Visual Studio, select File > Close Solution from the menu to close the Harmony Core solution.

  3. In the command prompt window, move to your main solution directory and execute the following command:

    harmonycore upgrade-latest
    

    You will be required to enter YES to confirm the upgrade.

  4. Close the command prompt, and then open the solution in Visual Studio.


Next topic: Configuring the Traditional Bridge Host Program


Clone this wiki locally