domenica 13 luglio 2008

ASP.NET, Web Services and Json

Web services offer a standardized mechanism to interface remote applications written in different languages.

They are based on two different protocols: HTTP for the tunnelling of the information and (usually, but we will show that this isn't the only possibility) XML for the coding of the information.

The protocol also requires the server exposes some information about the format of every possible functions so the client autonomously can compose and send data requests.

The best scenario where you can implement web services is client-server applications over Internet.

The server side usually needs a web server and the service is written as a web dynamic page in PHP, ASP.NET, JSP or similar languages/environments.

The client side could be a stand alone application, or, better, a web application running in a browser, in this case the compatibility with the widest range of browsers/operating systems restricts the choice to 3 solutions: a Flash plug-in, a Java applet or a Javascript application (and in the future Silverlight?).

I don't like Java applets (I'm looking to definitely remove JVM from my notebook) and I don't know Actionscript (the language for Flash), so the only choice appears to be Javascript.

In my opinion Javascript offers few advantages: it is implemented in every modern browser without installing anything else, it is quite standardized and, luckily, it offers the HttpXmlResponse class to retrieve remote data through a HTTP request. But it is interpreted and it doesn't offer native functions managing XML files, so the result is that Javascript applications exchanging XML-formatted data could be very slow.

Is XML the only data coding system on web services? No, it isn't: Json is another [great] possibility!

What is Json? Json - similarly to XML - is a method to code structured data, but it reproduce a subset of the Javascript syntax of declaration and initialization of variables, arrays and objects, so it's possible to decode Json files simply with the eval() native Javascript function: easy and fast.

But this is not all: Visual Studio NET 2008 offers the possibility of implement Json web services and, client-side, encapsulates the remote functions of the web services in pure-Javascript local function entry points. Great!

Implementing Json web services in ASP.NET is very easy:


- open Visual Web Developer 2008 Express Edition
- create a new ASP.NET project
- create a "Service" directory
- inside "Service" create an "AJAX-enabled WCF Service" file named "Json.svc"
- open "Json.cs" and modify the line "[ServiceContract(Namespace = "")]" to "[ServiceContract(Namespace = "Test")]"
- in "Json.cs" add "using System.Collections;" the public method "GetArrayList()" (who makes and return a populated ArrayList)
- to "Default.aspx" a ScriptManager:

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Services/Json.svc" />
</Services>
</asp:ScriptManager>

- to "Default.aspx" add a label (lblTest):
<asp:Label ID="lblTest" runat="server" Text=""></asp:Label>

- to "Default.aspx" add the Javascript code that connects to the remote web service:

<script language="javascript" type="text/javascript">
var x = 0;
function getal()
{
Test.Json.GetArrayList(onMethodCompleted);
}

function onMethodCompleted(results)
{
$get("lblTest").innerText = "Element " + x + ":" + results[x++];
}
</script>

- to "Default.aspx" add a button (with onclick event):

<asp:Label ID="lblTest" runat="server" Text="PUSH"></asp:Label><br />

Run and try!

Download TestJson.zip sample

Some screenshots of the output (for Chandra):



2 commenti:

chandra ha detto...

I liked your post but can you please post the screenshots of the output
Thanks

Explobot ha detto...

I added some screenshots at the end of the post.