Simultaneously calling multiple methods on a WCF service from silverlight

A while back I had to debug some performance issues in an existing Silverlight app, as the problem / solution was a bit obscure and finding info about it was quite tricky, I thought I’d share, maybe it can help the next person with this problem.

The App

On start, the app would do a number of calls to different methods on a WCF service, this to populate the UI with the necessary data. Recently one of those services had been changed and was now taking quite a bit longer than it used to. This was resulting in quite a long loading time for the whole UI, which was set up so it wouldn’t let the user interact with anything, until all the service calls had finished.

First I broke out the longer running service call from the others, then removed the constraint that it had to be loaded for the UI in general to become responsive. I also added a loading indicator just on that area of the UI, thinking that the main UI would load while this particular section could keep loading independently.

The Problem

However this is where things started to get a bit strange. I found that even after these changes, the main UI wouldn’t activate until the long running call returned. So now, I did what I should have done to start with, I got Fiddler out and had a look at what was really happening.

What I found was that, once the call to the long running service method was placed, all subsequent call were waiting for that one to return before executing.

LongRunningCall

Not having really worked with WCF previously or knowing much about it in general, I was stumped… I knew of the issues where Silverlight is restricted by the browsers networking features in regards to number of simultaneous connections etc. However that just didn’t seem to be the issue here, you can clearly see in Fiddler that there’s numerous calls, but they’re just not returning. I thought of the problem maybe being in the WCF service, but the calls were really not that complicated and surely the service should be able to handle a lot more than what I was throwing at it!

So I did what every developer does in this type of scenario, I hit the search engines. I did a whole bunch of searching on things like “multiple simultaneous WCF calls from Silverlight” and “Calling long running WCF services from Silverlight” etc. etc. This however, pretty much got me nowhere, I found a whole heap of resources on how to do WCF calls from Silverlight but most of them were very basic and of no use what so ever.

The fog is clearing

It wasn’t until I came across the term “ WCF blocking calls” and started incorporating that in my searches I started to get somewhere. Those searches quite quickly brought me to the following thread in the Silverlight forum “Long-running WCF call blocking subsequent calls” which discussed the exact problem I was facing and the best part, one of the guys there had the solution!

The short answer is in the forum post and the guys answering, has also done a more extensive blog post about it called “Silverlight, WCF, and ASP.Net Configuration Gotchas” which covers it very well. 

So come on what’s the solution?! I heard you ask, unless you’ve already gone to the links and looked it up ;)

The Solution

Well, it turns out that the issue is founded in a mix of Silverlight, Asp.Net and WCF, basically if you’re doing multiple calls to a single WCF web-service and you have Asp.Net session state enabled, the calls will be executed sequentially by the service, hence any long running calls will block subsequent ones. So why is Asp.Net session state effecting us, we’re working in Silverlight, right? We'll as mentioned earlier, by default Silverlight uses the browsers networking stack when doing service calls, hence to the WCF service, the call looks like it might as well be coming from a normal Asp.Net. To get around this, we look to a feature introduced in Silverlight 3, namely the Client HTTP Stack.

The Client HTTP Stack to the rescue

By using the following syntax (for example in our App.xaml.cs, Application_Startup method)

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

we can set our Silverlight application to use the Client HTTP Stack, which incidentally solves our problem! By using Silverlights own networking stack, rather than that of the browser, we get around the Asp.Net - WCF session state issue.
The above code specifies that all calls to addresses starting with “http://” should go through the client stack, this can actually be set more granular and you can specify it to be used only for certain domains etc.

Summary

The actual solution is well covered in the forum and blog posts I link to above. This post is more about sharing my experience, hopefully helping to spread the word about this and maybe make it a bit easier for the next poor guy with this issue to find the solution.

Until next time,
Ola

7 Comments

  • Nice one - I'd been stuck on this problem for days! Excellent article. In case this helps someone else, the easiest solution is to take

    aspNetCompatibilityEnabled="true"

    out of the Web.config of the site containing the WCF methods.

  • I tried the above solution with Client HTTP stack, but the moment i set an object to a Session, the calls are blocking. Until then the calls are asynchronous. any way to fix this issue ?

  • It doesn't work for me. Whether I enabled the client HTTP stack or not, my WCF service calls block all further wcf or ria service calls, and also blocks the UI until the initial call completes.

  • .NetDeveloper Sorry to hear it's not working for you. Unfortunately I'm no expert in WCF, so I can't help you there :( Hope you find a solution!
    /Ola

  • .NetDeveloper and Josh Sorry to hear it's not working for you guys. Unfortunately I'm no expert in WCF, so I can't help you any further, sorry :(

    Hope you find a solution!

    /Ola

  • THANKS! it works, it realy helps me

  • It doesn't work after string:

    HttpContext.Current.Session["qwe"] = 123;

    PS: SL5+WCF

Comments have been disabled for this content.