Saturday 17 July 2010

How Web services works internally?

But now imagine that your boss come to your office and requires new 3-tier architecture so that your Views and Controllers are on a frontend web server, your Model is on an application server and your database is on a database server. How would you achieve such architecture? How would you call model methods when it is running on different machine? There is a lot of technologies which allow you to call object from different process and even different machine. In the world of .NET we used to use .NET remoting or ASP.NET Web Services (ASMX) for calling remote objects. Now new applications are usually implemented by Windows Communication Foundation (WCF). ASMX and WCF are API for creating Web services.

Web services are development approach to call remote objects, moreover there is a lot of standardized protocols which allow interoperability. Interoperability means that your model can be written in Java and exposed as Java Web service but you will be able to consume web service in your VB.NET code. Now you probably asks what does consuming mean and how does it really work?

Consuming web service = using web service. Common approach to use a web service is based on a proxy pattern. The web service communication differs two players - a server (machine / process) exposing the service and a client (machine / process) consuming the service. The client contains proxy. From the developer point of view, public interface (available methods) of the proxy looks exactly same like public interface on the remote object. So the developer calls methods on the proxy and it internally communicates with the remote object - the service. In the most common communication pattern the proxy wraps method call into request and sends it over transport protocol to the service. Service extracts the content of the request and use it to fill parameters of exposed method, executes method and returns result as a response to the proxy. Proxy takes response, and returns response content as method call result. So using proxy is absolutly transparent and it looks like the remote object lives in the same process (except much longer execution times of the remote call). This is how web services work internally.

Creation of web service and its proxy from scratch is quite hard task because it requires deep knowledge of many protocols and other stuff so we are back in provided API - ASMX and WCF. These API allows you to define web service in extreamly simple way and proxy is generated automaticaly based on service description. So if you are developer that has to consume service in his application you will just use Add web reference or Add service reference in Visual Studio and it creates proxy together with all required classes (request parameter types and response types) for you.

I definitely recommend you to learn how to create service and how to consume it. Based on your experience with ASP.NET I would quickly go through ASMX web services and later on I will go to WCF wich is much more complex. I don't know what to recommend for learning ASP.NET Web Services. You should check MSDN and look for web casts if there is any available. For learning WCF I definitely recommend: http://www.amazon.com/Learning-Hands-Michele-Leroux-Bustamante/dp/0596101627/ref=sr_1_1?ie=UTF8&s=books&qid=1277756654&sr=8-1 and set of 15 web casts which corresponds to the content of this book: http://www.dasblonde.net/2007/06/24/WCFWebcastSeries.aspx

Best regards,
Premjit