Create a Service Instance

A service instance is a single-use object used to call a remote web service.

You can use the dw.svc.LocalServiceRegistry.createService() method to create a service instance. This method takes two arguments:

  • The ID of a service configured in Business Manager
  • A configuration object with callback handlers that you implement

The callbacks contain code that is executed at appropriate times by the service framework. For example, an HTTP or FTP service instance typically implements three callbacks: createRequest, parseResponse, and (for testing) mockCall.

A SOAP service implements two more callbacks: initServiceClient and execute.

Example: Create an FTP Service Instance

The following FTP service instance executes a single FTP operation (list):


 //LocalServiceRegistry.createService creates an instance of MyFTPService
 
 var myFTPService = LocalServiceRegistry.createService("MyFTPService", {
      
     // Three callback handlers are invoked by the Service Framework
     // for FTP services at appropriate times
     
     // Callback handler to construct request

     createRequest: function(svc:FTPService, params) {
         svc.setOperation("list", "/");
     },
     
     // Callback handler to parse response

     parseResponse : function(svc:FTPService, params) {
         var x : Array = [];
         var resp : Array = params;
         for(var i = 0; i < resp.length; i++) {
             var f = resp[i];
             x.push( { "name": f['name'], "timestamp": f['timestamp'] } );
         }
         return x;
     },

     // Callback handler to test the service using a mock response

     mockCall : function(svc:FTPService, params) {
         return [
             { "name": "file1", "timestamp": new Date(2011, 02, 21)},
             { "name": "file2", "timestamp": new Date(2012, 02, 21)},
             { "name": "file3", "timestamp": new Date(2013, 02, 21)}
         ];
     },
 });