Re: Re: Re: Getting the actual Exception thrown from a WebService (not just a 404 / ProtocolException)
You need to download the sample code from this link:
http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=silverlightws&DownloadId=3473
And run the SilverlightRawFault sample project you can see what I mean this is the best solution so far for handling the error thrown from WCF. If you take a look at WCF Service.svc.cs code you can see the DoWork function just throw an exception. There is nothing special in the service code. You just write your service code as you normally do. But the error thrown from WCF will be catched in the Silverlight end. All you need to do is the following:
1) Add reference of SilverlightFaultBehavior.dll (which is included in the sample files) to the Web project, and add the following behavior to the Web.config file:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="silverlightFaults" type="Microsoft.Silverlight.Samples.SilverlightFaultBehavior, SilverlightFaultBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="SilverlightFaultBehavior">
<silverlightFaults/>
</behavior>
</endpointBehaviors>
...
</system.serviceModel>
2) Add reference of SilverlightMessageInspector.dll (included in the sample) to the Silverlight project.
3) Add SilverlightFaultMessageInspector.cs (included in the sample) to your Silverlight project or your Silverlight Library project.
4) Change your WCF calling code to the following:
YourServiceClient proxy = new YourServiceClient(); // Your WCF service default consturctor
EndpointAddress address = new EndpointAddress("http://localhost:52620/Service.svc");
BasicHttpMessageInspectorBinding binding = new BasicHttpMessageInspectorBinding(new SilverlightFaultMessageInspector());
ServiceClient proxy = new ServiceClient(binding, address); // Use this WCF service constructor, pass new binding
proxy.DoWorkCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_DoWorkCompleted);
proxy.DoWorkAsync(); //code to call WCF
void proxy_DoWorkCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
if(e.Error is RawFaultException) // Now you can catch the Exception thrown from WCF
{
RawFaultException exception = (RawFaultException)e.Error;
MessageBox.Show("Service says: " + exception.FaultMessage + Environment.NewLine +
"Exception type: " + exception.FaultType + Environment.NewLine +
"Stack trace: " + exception.StackTrace);
}
else // None WCF error
MessageBox.Show(e.Message);
}
sladapter
Software Engineer
Aprimo, Inc
Please remember to mark the replies as answers if they answered your question