Showing posts with label services. Show all posts
Showing posts with label services. Show all posts

Oct 25, 2007

Introducing Basic Back-End Services: From AJAX to Silverlight

My previous post describes the overall problem of communicating with services from Silverlight. Let's now concentrate on one specific but very important part of it: "basic back-end services" (and by the way, if you can think of a better name for this please let me know :)

What I mean by "basic back-end services" is services that you write specifically for your Silverlight app.

Let's take an example: Suppose you want to create an interactive shopping application (something like this or even this). Your users will search for products, add them to a shopping cart, enter payment information, and so on. Clearly some information needs to flow between the browser and the back-end server for this to work.

Traditionally (in the pre-AJAX world) if you wanted to do this on the Microsoft platform, you would create a bunch of .aspx pages in ASP.NET that would communicate with the server using postbacks. When a user entered a search string and hit "Search", it would generate a POST to the server, which would then respond with a newly-generated page.

AJAX eliminated the need for page refreshes, and introduced the notion of "back-end services". These services process very specific information and return very specific replies instead of resending the entire page after every request. In this model, when the user hits "Search", a request is sent to the back-end service which replies with just the search results (instead of an entire HTML page that happens to contain the results). It is the job of JavaScript code to then integrate these results with the rest of the page.

Again, if we look at the Microsoft platform, ASP.NET AJAX makes this very easy with the UpdatePanel control: you don't need to write any code and you don't have to even know that these back-end services are there - you can think of them as being created automatically for you.

There are limits to what the UpdatePanel can do, and so to get more flexibility you will need to write custom services (and the JavaScript code that uses them) yourself. With ASP.NET AJAX, there are different options to write them (page methods, .asmx services, WCF services), but it all essentially boils down to the same thing: you are using JavaScript to remotely call a method. The "Search" button on your shopping site will essentially call something like GetProductList("searchString") on your back-end server, "Add To Cart"will call something like AddToCart(productId), and so on.

Notice that these back-end methods are very specific to your particular application - they are normally written specifically for this purpose and not reused for anything else. This is exactly the basic back-end services that this post is about. The opposite of this case is the interoperability problem - talking to services that have already been established for purposes other than your specific application and that you cannot change - but I will cover that separately.

So how do basic back-end services work in Silverlight? Well, in 1.0 you cannot call them from within the Silverlight app itself - your only option is to use JavaScript on the page that hosts your Silverlight control and use all of the AJAX techniques I described above.

In 1.1, you will be able to call services directly from .NET code in your Silverlight application. I will explain how in the next post.