Skip to content

Tutorial 04 09 Add Controller Class

Steve Ives edited this page May 27, 2020 · 22 revisions

Harmony Core Logo

Exposing Endpoints for Traditional Bridge Routines

You're almost there, only two more steps to go, the next of which is to actually expose your Traditional Bridge routines as endpoints of your web service.

There are several ways that you might choose to do this, depending on what your routines do, and how they may or may not relate to existing functionality exposed by existing controllers in your service.

For example, if your service is exposing OData endpoints for Orders, and your Traditional Bridge routines also expose functionality related to Orders, you may choose to expose them via custom endpoint methods on your existing OrdersController class.

But it may be that your Traditional Bridge functions are not related to other controllers in your service. In that case, it probably makes sense to simply add a new custom controller class. That's the approach we'll take here.

Add a Custom Controller Class

  1. In Solution Explorer, right-click on the Services.Controllers class and select Add > Class.

  2. Name the new class source file TraditionalBridgeController.dbl and click the Add button.

  3. Copy and paste the following code into the new class, replacing the default code:

    ;;*****************************************************************************
    ;;
    ;; Title:       TraditionalBridgeController.dbl
    ;;
    ;; Description: WebAPI controller to expose example Traditional Bridge Routines
    ;;
    ;;*****************************************************************************
    
    import Microsoft.AspNetCore.Authorization
    import Microsoft.AspNetCore.Mvc
    import Microsoft.Extensions.Configuration
    import Microsoft.Extensions.Options
    import Newtonsoft.Json
    import System
    import System.Collections.Generic
    import System.Linq
    import System.Text
    import System.Threading.Tasks
    
    import Services.Controllers
    import Services.Models
    
    namespace Services.Controllers
    
        ;{Authorize}
        {Route("TraditionalBridge")}
        public partial class TraditionalBridgeController extends ControllerBase
    
            ;; Services provided via dependency injection
            private _TraditionalBridgeService, @TraditionalBridgeService
    
            public method TraditionalBridgeController
                aTraditionalBridgeService, @TraditionalBridgeService
            proc
                _TraditionalBridgeService = aTraditionalBridgeService
            endmethod
    
    
    
        endclass
    
    endnamespace
    
    

As you can see, the new class inherits from a base class named ControllerBase (actually Microsoft.AspNetCore.Mvc.ControllerBase), which in a nutshell means that it is an MVC Controller (previously referred to as a WebAPI controller).

Again, the dependency injection pattern is being used, this time to get hold of an instance of a class named TraditionalBridgeService. Does that sound familiar? It should, it's the class that you wrote in the previous module of this tutorial, the one that exposes "wrapper" methods for your Traditional Bridge routines. Yes, it's all starting to come together!

Notice also that the class is decorated with an {Authorize} attribute, but it is commented out. You should change this if authentication is enabled in your environment.

  1. If your environment uses authentication (or custom authentication) then remove the comment from the {Authorize} attribute.

Notice also that the controller class is also decorated with a {Route("TraditionalBridge")}. This specifies that the endpoints in the controller will be available at the base address of the service, which on this occasion will NOT include the /odata/v1 part, because this controller is an MVC controller, not am OData controller.

  1. Save the code

Adding Endpoint Methods

Similar to when you built the TraditionalBridgeService class, the next task is to add an endpoint method for each of the three Traditional Bridge routines being exposed:

Adding the GetEnvironment Endpoint Method

  1. Copy and paste the following code into the TraditionalBridgeController class, between the endmethod and endclass statements:

    {Route("GetEnvironment")}
    public async method GetEnvironment, @Task<IActionResult>
    proc
        mreturn ok(await _TraditionalBridgeService.GetEnvironment())
    endmethod
    

As you can see, this endpoint is available at the URL TraditionalBridge/GetEnvironment, and simply makes a call to the appropriate service wrapper method, returning to the client whatever string value comes back, embedded in a 200 OK response. In this case that's really all that is needed, there really are no other valid response codes, and if there is a serious failure of any kind, MVC will trap the exception and return an HTTP 500 Internal Server Error to the client.

  1. Save the code

Adding the GetLogicalName Endpoint Method

  1. Copy and paste the following code into the TraditionalBridgeController class, between the endmethod and endclass statements:

    {Route("GetLogicalName/{aLogicalName}")}
    public async method GetLogicalName, @Task<IActionResult>
        required in aLogicalName, string
    proc
        mreturn ok(await _TraditionalBridgeService.GetLogicalName(aLogicalName))
    endmethod
    

This endpoint method is very similar. The URL route is different of course, and there is now a parameter. This means that requests to the service must look something like this:

TraditionalBridge/GetLogicalName/DBLDIR

Where DBLDIR is the name of the server-side logical name that we wish to retrieve the value for.

  1. Save the code

Adding the AddTwoNumbers Endpoint Method

  1. Copy and paste the following code into the TraditionalBridgeController class, between the endmethod and endclass statements:
{Route("AddTwoNumbers/{aNumber1}/{aNumber2}")}
public async method GetAddTwoNumbers, @Task<IActionResult>
    required in aNumber1, decimal
    required in aNumber2, decimal
proc
    mreturn ok(await _TraditionalBridgeService.AddTwoNumbers(aNumber1,aNumber2))
endmethod

And again, this third endpoint method is similar. Again a different URL route, and this time two query string based parameters, meaning that requests need to look something like this:

TraditionalBridge/AddTwoNumbers/1.1/2.2

Where 1.1 and '2.2' are the numbers that we wish to add together.

  1. Save the code

Build the Code

Before moving on, make sure the project builds:

  1. Right-click on the Services.Controllers project and select Build.

  2. Check the Output window and verify that the build was successful.

    1>------ Build started: Project: Services.Controllers, Configuration: Any CPU ------
    ========== Build: 1 succeeded, 0 failed, 2 up-to-date, 0 skipped ==========
    

Next topic: Configure Traditional Bridge Environment


Clone this wiki locally