Introduction and Notes about WCF, Silverlight and Creating a Silverlight application that consumes – invokes a WCF service via async (asynchronous) web service call

This post is an introduction and a collection of some useful information, notes and facts about WCF in genreal, Silverlight and running a silverlight web application that uses a WCF service via asynchronous web service call.

– The first fact that you need to know is that:

The only option for web service calling in Silverlight is asynchronous and Silverlight framework does not provide any API for synchronous call. Saying that, there are few work arounds for synchronous web service calls from silverlight to a WCF service. Personally, I haven’t tried or need to use any of these. If you are interested, you can look at one of these posts:
 * http://weblogs.asp.net/razan/archive/2010/01/14/emulating-synchronous-web-service-call-in-silverlight.aspx
 * http://marcgravell.blogspot.com/2009/02/async-without-pain.html
 * http://petesbloggerama.blogspot.com/2008/07/omg-silverlight-asynchronous-is-evil.html
– The greatest advantage of calling methods asynchronously is because it enables the application to continue doing useful work while the method call runs. If there is any delay from the WCF service in sending back the required data, Async calls ensures that your client application is not hanging there freezing and doing nothing until the Windows Communication Foundation (WCF) services returns the requried data. WCF and clients can participate in asynchronous operation calls at two distinct and different levels of the application. This makes WCF applications flexibile to maximize throughput balanced against interactivity.

– Visual Studio .NET 2008 gives you two project templates to create a WCF project:
 1- “WCF Service Application” and
 2- “WCF Service Library.”

WCF Service Application is the conventional web service application similar to ASP.NET web service but in this case built on Windows Communication Foudnation technology.

WCF Service Library project is different. The output of this project is a compiled dll file(s) along with a dll.config configuration file. The produced output can then be added to a client service application or project, deployed as a separated secure web service or as part of a larger hosted web service.

– When creating a WCF service for a silverlight application, you can simply add the WCF service to the test Web project which is added by default by Visual Studio when you create a new Silverlight 3 project. You can do this by right clicking on the silverlight web project (test project usually called .Web) and then click on add new item. In the new item window, always choose in this case silverlight enabled WCF service as this adds one line to your web.config. On the other hand, you can add the WCF service as another project to the same solution or even a separate solution which is the more practical approach as normally the web service is separate and hosted independantly from its client(s).

– If the WCF client application is not silverlight, opt to use the asynchronous approach in your operation implementation especially in the case when the service implementation makes a blocking call, such as doing Input/Output IO work.

Generally, even if you have a choice to choose between a synchronous and asynchronous call, always try to opt for the asynchronous one.

MSDN (and Microsoft) specifies the essential cases that you need to use Asynchornous calls to WCF as follows:

* If it is a silverlight application (the only option you have really!).

* If you are invoking operations from a middle-tier application.

* If you are invoking operations within an ASP.NET page, use asynchronous pages.

*- If you are invoking operations from any application that is single threaded, such as Windows Forms or Windows Presentation Foundation (WPF).

In all these cases, always try to opt to Async calls rather than synchronous if this is an option.
– WCF is Microsoft’s attempt of unifying web and distributed services. WCF services can work with a variety of client applications including php, ruby, etc. and any client that can use basicHTTP binding.

– WCF is regarded as the successor to conventional Microsoft web services and communication technologies such as: DDL, DCOM, Remoting, Web Services, WSE, etc.. This is especially because WCF is built with the aim to provide “SOA” or Service Oriented Architecture for distributed applications.

– WCF can have messages sent in a variety of channels including HTTP, TCP, MSMQ, Named pipe, etc.

– WCF runtime resides under the System.ServiceModel namespace.

– In most cases WCF has a much better and faster performance ranging between 25%-50% in some cases. Refer to this MSDN article for some comparisons: http://msdn.microsoft.com/en-us/library/bb310550.aspx

– The throughput of WCF is inherently scalable from a single processor to a quad processor.

– The WCF model unifies the feature wealth of ASMX, WSE, Enterprise Services, MSMQ, and Remoting. This way, developers only have to master a single programming model.

– WCF can be hosted in IIS Servers, Windows services and standalone apps like windows forms, console apps.

– WCF provides a DataContractSerializer which allows complex data types and private attributes to be serialized and sent. Being Serializable means, the object can be hydrated, dehydradted from a stream/bunch of bytes, into a living class instance.