Tuesday, October 23, 2007

The Beauties of PageMethods & WebServices

It's funny. My first experiences with AJAX was back with the old XMLHTTP Post days going to a Java Servlet. I liked the results but hated the implementation. Thankfully the rest of the development community and vendors couldn't either, and now, today, you can't go anywhere without hearing AJAX it seems.

I didn't care too much for Microsoft's ASP.Net 2.0 AJAX implementation. I toyed around with it and like the simplicity of Update Panels; however, I hated the overhead and the synchronous interaction they provided. I was looking for an easy toolkit that I can use w/o having to write a lot of custom JavaScript or focus on writing my own JavaScript toolbox like I felt like I had to with AJAXPro. Luckily, I stumbled upon different features of ASP.Net AJAX; PageMethods and Web Services.

Most blogs about ASP.Net 2.0 AJAX will focus on the uses of Update Panels or controls from the toolkit. However, there's a couple that talk about the ability to do truly Asynchronous calls using PageMethods or WebServices. Under the hood, these appear to be pretty much the same: we public-scoped, static/shared method that can be called using JavaScript once it's class has been registered with the ScriptManager (or ScriptManagerProxy in the case of Master Pages).

With the similarities, there are some differences:

  • Page Methods are Public, Static methods located in the page's class file or in it's <Script Runat=Server> tag.

  • PageMethods are scoped to just the local page. You cannot call a page method of a different page than what you are currently on. (I'm needing to test this further.)

  • Web Services methods allow for a better separation of presentation and control than what a normal code behind structure can provide.

  • You cannot reference a WebService method that is in a different domain than the page. (Cross Domain Scripting has security issues that many modern browsers just shut down now).



With two different technologies, which is the better one to use inside of your application?

For the PageMethods model:


  • Best for smaller projects

  • If time is critical, Page Methods are faster to develop then the WebService Model



For the WebService model:


  • When the same code is needed in multiple locations.

  • Separation of the control logic is desired

  • Projects are larger and tends to find multiple developers working amongst each other's code



In the next post, I'll provide some code snippets on how to get started on each of these technologies.

No comments:

Post a Comment