Navigation:  Getting Started with WebORB >

JavaScript (AJAX) and WebORB

Previous pageReturn to chapter overviewNext page

The instructions below will guide you through the process of integrating a JavaScript client with .NET backend using AJAX and WebORB support for JavaScript applications.

 

The code used in this quick start guide is available in your WebORB distribution at:

 

JavaScript source:                 [WEBORB HOME]/examples/quickstart/javascript-ajax
C# code/compiled assembly:         [WEBORB HOME]/examples/quickstart/server-side

 

The default location of [WEBORB HOME] is /Program Files/WebORB for .NET/4.0/

 

.NET Service

The server side code for this quick start can be found at the following path in your WebORB installation: [WEBORB HOME]\examples\quickstart\server-side. The directory also includes a compiled assembly (quickstart.dll). You can either compile the code yourself, or use quickstart.dll included with the product distribution. To expose the server-side class as a remoting service through WebORB, the assembly must be copied to the /bin folder of your WebORB installation. Once you deploy the assembly, you can verify that it is recognized by WebORB by opening the WebORB Management Console and selecting the Services tab. The assembly will show up in the ".NET Assemblies" node in the service browser.

 

The source code for the server-side class is below:

using System;
using System.Web;
using Weborb.Util;
 
namespace weborb.examples.quickstart
{
 public class ComputerInfoService
 {
   public ComputerInfo getComputerInfo( String requestId )
   {
     ComputerInfo computerInfo = new ComputerInfo();          
     System.Web.HttpRequest request = ThreadContext.currentRequest();
     computerInfo.serverName = request.ServerVariables[ "SERVER_SOFTWARE" ];
     computerInfo.clrVersion = Environment.Version.ToString();
     computerInfo.clrVendor = "Microsoft";
     computerInfo.os = Environment.OSVersion.Platform.ToString();
     computerInfo.osVersion = Environment.OSVersion.Version.ToString();
     computerInfo.requestId = requestId;
     return computerInfo;
   }
 }
 
 public class ComputerInfo
 {
   public string serverName;
   public string clrVersion;
   public string clrVendor;
   public string os;
   public string osVersion;
   public string requestId;
 }
}

 

JavaScript Client

WebORB for .NET supports AJAX-based invocations of .NET objects. The product includes a JavaScript library enabling the integration on the client (browser) side. This quick start guide includes a working example in the product distribution at [WEBORB HOME]/examples/quickstart/javascript-ajax.

To enable JavaScript/AJAX to .NET integration, the JavaScript code must import the WebORB client-side library:

<script language="javascript" src="WebORB.js"></script>

The sample client uses the following code to connect to WebORB and perform a remoting (AJAX) invocation:

var proxy;
var requestID = 1;
 
function invokeServer( syncMode )
{
 proxy = webORB.bind( "weborb.examples.quickstart.ComputerInfoService", "weborb.aspx");
 var responseObj;
 
 if( syncMode )
 {
   responseObj = proxy.getComputerInfo( requestID++ );
   handleResponse( responseObj );
 }
 else
 {
   proxy.getComputerInfo( requestID++, new Async( handleResponse ) );
 }
}

The webORB.bind() call connects to WebORB and creates the JavaScript proxy for the .NET class identified by the first argument of the call ("weborb.examples.quickstart.ComputerInfoService"). The class must be identified by using full .NET type name (namespace plus class name). The second argument ("weborb.aspx") is a  URL for WebORB. In the example above, the URL is a relative URL, as a result, the JavaScript client must be deployed in and loaded from the same virtual directory where WebORB is running. Alternatively, the URL can be an absolute URL if the client is loaded from elsewhere.

 

Once the JavaScript proxy for a remote class is created, you can use it to execute remote method invocations. The example above shows two ways to invoke remote methods - synchronous and asynchronous. To invoke a remote .NET method, simply invoke it on the proxy object and pass the same arguments the method on the server expects. In the example above, the server-side method signature of getComputerInfo declares one argument - requestID, the JavaScript code sends a corresponding numeric value.

 

For asynchronous invocations, the code creates an instance of the Async class and specifies the name of the callback function. The callback function is invoked by the WebORB JavaScript library when a response from the remote invocation becomes available.

 

Deploying and Running the JavaScript Client

To deploy the JavaScript client for the example reviewed in this guide, copy computerinfo.htm into the root of the weborb4 virtual directory (the default location is /Program Files/WebORB for .NET/4.0/). You can run the JavaScript client by opening the following URL: http://localhost/weborb4/computerinfo.htm