Nerdy tidbits from my life as a software engineer

Monday, July 13, 2009

WCF Duplex Bindings With Silverlight

I have had the hardest time getting my self-hosted WCF service to play nicely with Silverlight.  I thought this was supposed to be simple, but it turns out that exposing a self-hosted, WCF service with a callback contract to a Silverlight application is just about impossible.  Here are the five hurdles you have to overcome to get this to work:

  1. First, you have to explicitly give your service permission to open an endpoint on localhost at a specific URL.  I’m sure this can be automated somehow, but probably not easily.
  2. You have to host a clientaccesspolicy.xml file on the service in order to give the Silverlight runtime permission to call your service.  This involves writing another WCF service just to return basic documents via HTTP.  It’s not too tough, but very annoying.
  3. Silverlight does not support the WSDualHttpBinding binding.  To get around this, the server needs to expose itself via a custom endpoint that is configured to use a PollingDuplexElement object.   How you would ever figure this out without the help of this MSDN article, I have no idea.
  4. Next, the Silverlight application needs to be configured with another custom binding that can communicate with this strange, bastardized endpoint that you have exposed on the server.  Svcutil.exe does not pick this up for you: you’ve got to define this endpoint manually.  Another MSDN article explains this nastiness.  Good luck finding this out by yourself.
  5. Whatever your ServiceContract you had before you decided to add a Silverlight client will now need to change to send and receive using Message objects.  For me, this was a deal-breaker.  All of your nice, strongly-typed DataContract’s go away and get replaced with generic SOAP messages.  Terrible.  And all of your other clients now need to be updated to deal with these objects instead of the strongly-typed data structures that make WCF so powerful to use in the first place.

All of these hurdles have caused me to put off the Silverlight client until further notice.  The only way I can think of writing this is to do a very nasty double-hop scenario.  And I really hate that idea.

0 comments: