Click here to Skip to main content
Click here to Skip to main content

Synchronous WCF Service Calls with Silverlight

By , 16 Mar 2010
 
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, 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
Member
I spent most of time on Silverlight, Xna (where i am MVP) and ADO.Net Data Services.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionnot work browser operamemberouadie9920 Feb '13 - 6:27 
GeneralMy vote of 5membernileshpatel1583@gmail.com2 May '12 - 20:39 
GeneralRe: My vote of 5membernileshpatel1583@gmail.com7 May '12 - 2:17 
QuestionIIs problem?membercoerrace12 Jan '11 - 8:22 
GeneralOut of Browsermemberdanparker27630 Aug '10 - 9:58 
QuestionHow to Synchronous Call WCF RIA Services in SL 4? [modified]memberMember 14670219 Aug '10 - 23:03 
GeneralMy vote of 5memberMember 14670219 Aug '10 - 2:45 
GeneralFYI: This has been around for yearsmemberDewey18 Jun '10 - 18:13 
GeneralSuperb!mvpDaniel Vaughan18 Mar '10 - 4:21 
GeneralRe: Superb!memberValentin Billotte18 Mar '10 - 5:13 

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

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