Click here to Skip to main content
13,866,965 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


34 bookmarked
Posted 16 Mar 2010
Licenced CPOL

Synchronous WCF Service Calls with Silverlight

, 16 Mar 2010
Rate this:
Please Sign up or sign in to vote.
How to make TRUE sync call in Silverlight
Clicking on one button freezes the Silverlight UI Thread, but not the browser.


This small article is dedicated to Daniel Vaughan, one of the greatest article writers on CodeProject. Yesterday, I saw one of his articles about synchronous call on Silverlight. I did not agree with his method which finally makes a synchronous call on an asynchronous thread. Most of the synchronous problems cannot work with it.

I have to make synchronous calls in my own application to create a communication pipe between my Silverlight app and an LMS server (exposing WCF services).

My Solution

My solution is very simple, I just use JavaScript, the Silverlight blood brother to make my WCF call with the help of the XmlHttpRequest object. I create a generic JS function that will call the specified method on a WCF frontage and add specified parameters.

I created a manager that calls this JS function and formats a simple JSON message.

The sample is very basic because I do not have a lot of time to improve it. It just works with string parameters and string returns. If you want to use WCF OperationContract with other object types, just use a true JSON serializer. :)

JavaScript Part

The function read the arguments passed by the manager with the arguments array:

function SyncWCFServiceCaller() 
var client = null;

if (window.XMLHttpRequest)
client = new XMLHttpRequest();
else if (window.ActiveXObject)
client = new ActiveXObject("Microsoft.XMLHTTP");
var data = arguments[1];'POST', arguments[0], false);
client.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
client.setRequestHeader('Content-Length', data.length + '');
var response = eval('(' + client.responseText + ')').d;
return response;

arguments[0] contains the WCF endpoint and the method name (such as "localhost/service.svc/method").

arguments[1] contains the serialized parameters (in JSON), like "{"name" : "daniel"}"

Nothing complicated here. The XmlHttpRequest creates a sync call so if it is called from Silverlight it does not return to the caller before the return of the service.

My work uses only the string object type so I just return the default 'd' JSON object that contains the result.

Silverlight Part

The C# manager is very simple too:

public class ContractToJavascriptCaller<T>
public string ServiceEndPointUri
private set;
public ContractToJavascriptCaller(string serviceEndPointUri)
this.ServiceEndPointUri = serviceEndPointUri;
public object CallMethod(string methodName, params string[] arguments)
StringBuilder builder = new StringBuilder();
MethodInfo methodInfo = typeof(T).GetMethod(methodName);
ParameterInfo[] parameters = methodInfo.GetParameters();
int parameterIndex = 0;
foreach (ParameterInfo parameter in parameters)

string jsonSerializedParameters = builder.ToString().TrimEnd(',');
jsonSerializedParameters = string.Concat(jsonSerializedParameters, "}");

return HtmlPage.Window.Invoke("SyncWCFServiceCaller", 
	string.Concat(this.ServiceEndPointUri, "/", methodName), 

This manager has just one method, CallMethod, that will invoke the JS function with the end point of the service and the method name, and the parameters serialized as JSON. The HtmlPage.Window.Invoke does not return before the WCF service call.

The CallMethod receives the WCF MethodName to call. It uses reflection to get the parameters and serializes their name and values in JSON.

That's all! If you call a WCF service in this way, you will freeze the UI Thread but not the Browser.

Calling a WCF Service

My WCF service contains two methods:

string DoWork(string name);
string DoWorkWithTwoParams(string name, string name2); 

I put a Thread.Sleep instruction inside their instruction block to simulate a big computing set of instructions. The Interface for the WCF service is linked inside the Silverlight project. So to call a WCF method, I just have to do:

ContractToJavascriptCaller<IDanielService> caller = 
new ContractToJavascriptCaller<IDanielService>(http://localhost:23770/DanielService.svc);
string response = caller.CallMethod("DoWork", this.firstMathodTextBlock.Text).ToString();
this.ResultTextBlock.Text = response; 

Sorry for:

  1. My poor English
  2. The bad quality of the code, I really do not have time for myself: I am currently listening to Mix 2010 session about WP7.


  • 15th March, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Valentin Billotte
Architect Viablue - GraphicStream
France France
I spent most of time on Silverlight, Xna (where i am MVP) and ADO.Net Data Services.

You may also be interested in...


Comments and Discussions

Questionnot work browser opera Pin
ouadie9920-Feb-13 7:27
memberouadie9920-Feb-13 7:27 
GeneralMy vote of 5 Pin
nileshpatel1583@gmail.com2-May-12 21:39
membernileshpatel1583@gmail.com2-May-12 21:39 
GeneralRe: My vote of 5 Pin
nileshpatel1583@gmail.com7-May-12 3:17
membernileshpatel1583@gmail.com7-May-12 3:17 
QuestionIIs problem? Pin
coerrace12-Jan-11 9:22
membercoerrace12-Jan-11 9:22 
GeneralOut of Browser Pin
danparker27630-Aug-10 10:58
memberdanparker27630-Aug-10 10:58 
QuestionHow to Synchronous Call WCF RIA Services in SL 4? [modified] Pin
Member 14670220-Aug-10 0:03
memberMember 14670220-Aug-10 0:03 
GeneralMy vote of 5 Pin
Member 14670219-Aug-10 3:45
memberMember 14670219-Aug-10 3:45 
GeneralFYI: This has been around for years Pin
Dewey18-Jun-10 19:13
memberDewey18-Jun-10 19:13 
GeneralSuperb! Pin
Daniel Vaughan18-Mar-10 5:21
memberDaniel Vaughan18-Mar-10 5:21 
GeneralRe: Superb! Pin
Valentin Billotte18-Mar-10 6:13
memberValentin Billotte18-Mar-10 6:13 
GeneralRe: Superb! Pin
Daniel Vaughan18-Mar-10 7:08
memberDaniel Vaughan18-Mar-10 7:08 
GeneralEnjoyed the article Pin
linuxjr16-Mar-10 13:17
professionallinuxjr16-Mar-10 13:17 
GeneralRe: Enjoyed the article Pin
Valentin Billotte16-Mar-10 22:31
memberValentin Billotte16-Mar-10 22:31 
GeneralLike it, like it Pin
Sacha Barber16-Mar-10 7:23
mvaSacha Barber16-Mar-10 7:23 
GeneralRe: Like it, like it Pin
Valentin Billotte16-Mar-10 8:26
memberValentin Billotte16-Mar-10 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 16 Mar 2010
Article Copyright 2010 by Valentin Billotte
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid