ARI Examples
Here I will demonstrate how to use ARI so you can see the clean syntax and just
how quicky you can enable an entire website using this library. You can also
download the software which include a website loaded with example code.
For each of the examples below, I will give a brief description of what you are
looking at, and include both the JavaScript code as well as the server-side code
in C#. Note that in these exmaples, a single error callback function called err_PageCallback
is used for brevity. This will suffice for many applications, but you can have a
different error callback for each of your calls.
Demo calling C# method that has a return type of void
In this example, the C# server-side method has a return type of void. Notice that
the callback is still called, but there is nothing being passed to the callback.
Demo calling C# method that has a return type of boolean
In this example, the C# server-side method has a return type of bool, which means
that the parameter passed to the success callback function is a boolean value as
well.
Demo calling C# method that has a return type of string
In this example, the C# server-side method has a return type of string, which means
that the parameter passed to the success callback function is a string value as
well.
Demo calling C# method that has a return type of integer
In this example, the C# server-side method has a return type of int, which means
that the parameter passed to the success callback function is a number value with
no decimal places
Demo calling C# method that has a return type of float
In this example, the C# server-side method has a return type of float, which means
that the parameter passed to the success callback function is a number value that
may have decimal places, if the actual return value from the server had decimal
places.
Demo calling C# method that has a return type of DateTime
In this example, the C# server-side method has a return type of DateTime, which
means that the parameter passed to the success callback function is a JavaScript
Date object.
Demo calling C# method that has a return type of Hashtable
In this example, the C# server-side method has a return type of Hashtable, which
means that the parameter passed to the success callback function is a JavaScript
object. Notice how nicely a C# hastable maps to a JavaScript object, with the keys
being properties and the values being - duh - values.
Demo calling C# method that has a return type of ArrayList
In this example, the C# server-side method has a return type of ArrayList, which
means that the parameter passed to the success callback function is a JavaScript
Array type.
Demo calling C# method that has a return type of DataTable
In this example, the C# server-side method has a return type of DataTable, which
means that the parameter passed to the success callback function is an array of
JavaScript objects. This actually works quite nicely, with each row mapping to a
JavaScript object. The column names in the row are the object properties, and the
actual data values are the values. In this example I created the DataTable manually,
but in practice, you would most likely be returning a DataTable that you got from
the database.
Demo calling C# method that has a return type of binary. (Base64 encoded.)
In this example, the C# server-side method has a return type of byte[] (binary data).
On the server side, ARI converts this to a base 64 string and sends it back to the
client callback as a base 64 string. In this example, as is the case in most,
I am simply demonstrating that data transmission and data conversion has taken place
successfully, and not really doing anything with the returned data. By the way,
to generate binary data, I just took an image file and read it into a byte stream.
Demo calling STATIC C# method that has a return type of string
In this example, the C# server-side method is a static that has a return type of
string, which means that the parameter passed to the success callback function is
a string value as well. This is just to demonstrate that ARI can execute static
methods as well as instance methods in the App_Code tree. Note that the methods
invoked are limited to those that have public scope.
Demo calling various overloaded C# methods
Note that the only difference between the four server side methods is the parameter
types they take. This demonstrates that using .NET reflection, ARI dynamically figures
out which of the server side methods to call. Refer to the discussion on narrowing
and widening coercion on the
ARI main page for an understanding
on how method arguments can be "promoted". This code, as well as most of the other
examples on this page, are included in the download.
Demo calling C# method that takes an Array as a parameter
In this example, the C# server-side method requires an argument of type ArrayList.
This means that the parameter passed from the JavaScript call should be an Array.
Demo calling C# method that takes a javascript object as a parameter
In this example, the C# server-side method requires an argument of type Hashtable.
This means that the parameter passed from the JavaScript call should be an object.
ARI will convert this to a HashTable using the properties as keys and values as
values.
Demo calling C# method that takes an array of javascript objects as a parameter
In this example, the C# server-side method requires an argument of type ArrayList.
This means that the parameter passed from the JavaScript call should be an Array.
This is similar to another example, except that I am showing the flexibility in
ARI to be able to nest objects to and from the server. Instead of a simple Array
being passed to the web server, we are passing an array of objects. Conceptually,
this can be considered a DataTable or a 2-dimensional array.
Demo calling C# method that throws an exception
In this example, I demonstrate what happens when/if the C# server-side method throws
an exception. The exception is gracefully caught by the ARI server side ServerAgent,
who then passes back the error message back to the fault handler specified as the
second parameter in the client side call. In this example, it is the error callback
err_PageCallback. Of course, in this example, the success callback, called cb_DemoMethodException
would never be executed since our server method always causes an exception
to be thrown.
Demo calling C# method takes nested hashtable as a parameter
In this example, the C# server-side method requires an argument of type Hashtable.
This means that the parameter passed from the JavaScript call should be of type
object. I kind of went crazy here, just nesting things and sending all kinds of
things to the server. I did this to demonstrate the recursive serialization and
deserialization I wrote into ARI. This feature is one of the most powerful things
about ARI. Very often I may need to send (or receive) more than type of data structure
to/from the server. What I usually do in that case is to build a self-describing
message. What I mean by that is I send a Hashtable whose keys describe the associated
data value. Very handy! (see the very next example for how to do this when the data
is coming from the server rather than going to the server.)
Demo calling C# method that returns hashtable with nested data structures
In this example, the C# server-side method is returning a Hashtable which itself
contains a DataTable, an an ArrayList. As illustrated in the previous example, this
is to show the recursive data serialization and deserialization capabilities of
ARI.
Demonstrates how ARI can execute 100 asynchronous calls
I put this example here just to demonstrate that ARI can handle multiple requests.
Of course, some browsers limit the number of concurrent calls oustanding at any
one time. ARI's JavaScript proxy code queues up the server calls and fires them
off as fast as possible. You really can't see much in the example code for this
methos, so I suggest you
download ARI and run it on
your machine. On my development box, this code executes the 100 round trip calls
very quickly - almost instantaneously.