Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Synchronous WCF Service Calls with Silverlight

0.00/5 (No votes)
16 Mar 2010 1  
How to make TRUE sync call in Silverlight
Clicking on one button freezes the Silverlight UI Thread, but not the browser.

Introduction

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];
client.open('POST', arguments[0], false);
client.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
client.setRequestHeader('Content-Length', data.length + '');
client.send(data);
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
{
get;
private set;
}
public ContractToJavascriptCaller(string serviceEndPointUri)
{
this.ServiceEndPointUri = serviceEndPointUri;
}
public object CallMethod(string methodName, params string[] arguments)
{
StringBuilder builder = new StringBuilder();
builder.Append("{");
MethodInfo methodInfo = typeof(T).GetMethod(methodName);
ParameterInfo[] parameters = methodInfo.GetParameters();
int parameterIndex = 0;
foreach (ParameterInfo parameter in parameters)
{
builder.Append("\"");
builder.Append(parameter.Name);
builder.Append("\"");
builder.Append(":\"");
builder.Append(arguments[parameterIndex++]);
builder.Append("\",");
}

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

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

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.

History

  • 15th March, 2010: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here