Geeks With Blogs
Adrian Hara Working through the .NET maze
This has probably been covered in some tutorial videos or "silverlight 3 what's new"s, but due to the fact that I spent the better half of an hour googling about ways to do it, I guess also posting it here for reference won't hurt.

Problem: when calling a WCF service from Silverlight 2, if the service throws an exception, in Silverlight you get a CommunicationException with some mumbo-jumbo in it, not the actual exception or Fault declared on the service.

Fix: in Silverlight 3 there is built-in support on the client-side for FaultException and FaultException<T>. All you need to do it tweak the WCF service a bit so it returns a 200 response code, instead of the default 500 response code, so you actually get the FaultException, as explained here: http://msdn.microsoft.com/en-us/library/dd470096(VS.96).aspx

Also, some more info on this and other new stuff in SL3: http://blogs.msdn.com/silverlightws/archive/2009/03/20/what-s-new-with-web-services-in-silverlight-3-beta.aspx Posted on Wednesday, April 29, 2009 12:25 PM | Back to top


Comments on this post: Handling WCF exceptions in Silverlight 3

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
Hi Adrian,
After spending the best part of half a day trying to get this to work I have had no joy.
The link is a little vague on the step by step instruction on what is required. i.e. create this library, add a ref to this project etc. Have you seen any other articles on this that go into more detail. Cheers
Left by Mark on Jul 21, 2009 11:38 AM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
Umm..not really, but the explanations worked for me. What's your exact problem?
Left by Adrian Hara on Jul 21, 2009 12:32 PM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
The <silverlightFaults/> element is not recognized even after the behaviorExtension has been added to the config.
Did you compile the SilverlightFaultBehavior as a separate dll or just add it as a class to your Service Project?
Left by Mark on Jul 22, 2009 1:32 AM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
Hi Adrian,
I have a similar query to Mark...

When I try to update my service, I get the error:
The type '....silverLightFaultBehavior....' registered for extension 'silverlightFaults' could not be loaded.

I have the silverlightFaultBehavior code file in the same project and namespace as the web app hosting the service.

Any idea what I'm doing wrong?
Left by dave on Jul 23, 2009 5:58 AM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
I'm out of the country currently, but next week latest I will take a look at the actual project and get back to you. Hope it's not too late...
Left by Adrian Hara on Jul 23, 2009 6:29 AM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
Ok, I looked:
1. I have it in a separate .dll, not in the hosting (web) project.
2. In visual studio, where the behavior extension is used (in the example below this is the "<silverlightFaults/>" element) it gets underlined with a blue squiggly line and the tooltip says it can't be found, however at runtime it works like a charm.

Here's my web.config relevant part from the hosting application:

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SDS.WCF.ServiceImplementation.DataService_Behavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="SilverlightFaultBehavior">
<silverlightFaults/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="SDS.WCF.ServiceImplementation.DataService_Behavior" name="SDS.WCF.ServiceImplementation.DataService">
<endpoint behaviorConfiguration="SilverlightFaultBehavior" address="" binding="basicHttpBinding" name="DefaultEndpoint" bindingNamespace="urn:Ssmt.SilverlightDataService.ServiceContracts:v1" contract="SDS.WCF.ServiceContracts.IDataServiceContract"/>
</service>
</services>
<extensions>
<behaviorExtensions>
<add name="silverlightFaults" type="SDS.WCF.ServiceImplementation.WCFHelpers.SilverlightFaultBehavior, SDS.WCF.ServiceImplementation.ServiceImplementation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
Left by Adrian Hara on Jul 27, 2009 1:27 AM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
Same problem here with the same error and same blue squiggly line. I have everything in the web project. Anyone able to get this to work?
Left by Dan on Jul 29, 2009 12:52 PM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
Nevermind, I got it to work. I put the SilverlightFaultBehavior in a separate assembly, referenced it in the website project, and also before rebuilding and trying out the service, I removed the existing SilverlightFaultBehavior.dll from the website's bin directory.
Left by Dan on Jul 29, 2009 1:53 PM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
Followed the instructions but in the client still the FaultException<> block was NOT hit. Instead, the Exception block was. Created the silverlightFaultBehavior.dll and modified the web.config . The debugger does go to SilverlightFaultBehavior . What am I missing?
Left by Priya on Sep 23, 2009 10:34 PM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
I wrote the end point behavior extension to handle faults as said in http://msdn.microsoft.com/en-us/library/dd470096(VS.96).aspx. But it is not working. When I debug the code, BehaviorType property is invoked but CreateBehavior method in IBehaviorExtensionElement is not invoked. Pls help to resolve it.
Left by Raj on Jan 27, 2010 12:17 AM

# re: Handling WCF exceptions in Silverlight 3
Requesting Gravatar...
Thanks, it works. No need for a separate dll here, just watch for the correct namespace.

<add name="silverlightFaults"
type="MyNamespace.SilverlightFaultBehavior, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
Left by Miran on Feb 02, 2010 6:29 AM

Your comment:
 (will show your gravatar)


Copyright © Adrian Hara | Powered by: GeeksWithBlogs.net