When you are developing some kind of Ajax powered web page and you need to transfer some data back and forth
between the web browser and the web server, you have more then one way to do it.
One way is to be lazy and do your job quickly using UpdatePanel. This is probably the easiest way, but it's certainly not
the best way. There is many posts about performance problems related with UpadatePanel
and here
is just one of them. If you care about the performance you will transfer your
data on some other way. ASP.NET AJAX offers two other ways to do it:
- calling web service methods
- calling page methods
There is also many posts about both of them and some people are saying that you should prefer web services, some are saying
that you should prefer page methods and some are saying that they are both the same. So, the questions is who
has right? Well, in last
few days I tested both of them and come to the conclusion that they are pretty much the same, but when performances are
really important, you should definitely prefer web services. There are two reasons for that:
- You can execute web service methods asynchronously
- In page methods you can't disable session, in web services you can
Page methods and web service methods are called asynchronously on the client, but on the server they are processed synchronously. This basically means
that ASP.NET will pick one CLR thread for every incoming request and will not release it and return it to the CLR tread pool as long as request is
processed. If request processing is fast you can live with it. But, if there are many concurrent requests and if requests processing involves
relatively long waiting for external I/O operations to complete (for example database queries) you will soon run into problems because there will not
be any free CLR threads to process other requests. If you have this kind of problem with the web forms you easily fix it using asynchronous pages.
Here is a nice explanation how to do it.
But, how can you make asynchronous web services and web methods? I don't know if you can do that with page methods,
but in Omar's book you can read how you can do that
with web services.
Asynchronous request processing is the first reason to prefer web service methods calls over page method calls. But, there is also one more reason
and it's related with the use of session. Using WebMethod attribute and its EnableSession property you can disable session on the web service method
level. You can try to do the same with the page methods, but session will
always be enabled and you will always be able to put some data in Session object or pull data from it.
That means that session will be loaded on every request which will downgrade
performances.