Solved

Silverlight and Webservice Exceptions

Posted on 2010-09-14
12
825 Views
Last Modified: 2013-11-12
I am using silverlight 4 and calling a web service. The web service validates my data against business rules and if one fails it throws an exception. In silverlight 4 is there any way to catch the exception. Currently the only detail being sent back is "Not found"

Thanks,
Jim
0
Comment
Question by:jimseiwert
  • 5
  • 5
  • 2
12 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33674520
you need to have fault hadling in your WCF service
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33674526
0
 
LVL 2

Author Comment

by:jimseiwert
ID: 33687734
I tried those but they did not work, Maybe I am missing something. below is an example of an error thrown in my webservice that I want to get the details of in silverlight. The throw exception detail is what I want to catch in silverlight on the return.
sub somefunction
       try
          'do something
       Catch ex As Exception
            Throw New Exception(ex.Message)
       End Try
end sub

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33689587
it should be Throw New FaultException(ex.Message)..if that doest work can you post you config file for service
0
 
LVL 2

Author Comment

by:jimseiwert
ID: 33693919
Ok I changed it to throw faultexception below is my web.config. Thank you again for looking at this
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="DomainServiceModule" preCondition="managedHandler" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <system.web>
    <httpModules>
      <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </httpModules>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add name="silverlightFaults"
          type="SilverlightFaultBehavior.SilverlightFaultBehavior,
        SilverlightFaultBehavior, Version=1.0.0.0,
        Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <connectionStrings>
    <add name="SNC_OperationsConnectionString3" connectionString="Data Source=snccostpoint;Initial Catalog=SNC_Operations;Persist Security Info=True;User ID=WT_Operations" providerName="System.Data.SqlClient" /><add name="SNC_OperationsConnectionString" connectionString="Data Source=snccostpoint;Initial Catalog=SNC_Operations;Persist Security Info=True;User ID=WT_Operations;Password=G&amp;E+KGxbg6WNjUu" providerName="System.Data.SqlClient" />
  <add name="SNC_OperationsEntities" connectionString="metadata=res://*/Operations.csdl|res://*/Operations.ssdl|res://*/Operations.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=snccostpoint;Initial Catalog=SNC_Operations;Persist Security Info=True;User ID=WT_Operations;Password=G&amp;E+KGxbg6WNjUu;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /><add name="SNC_OperationsEntities1" connectionString="metadata=res://*/Operations.csdl|res://*/Operations.ssdl|res://*/Operations.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=snccostpoint;Initial Catalog=SNC_Operations;Persist Security Info=True;User ID=WT_Operations;Password=G&amp;E+KGxbg6WNjUu;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /><add name="SNC_OperationsEntities2" connectionString="metadata=res://*/OperationsDataModel.csdl|res://*/OperationsDataModel.ssdl|res://*/OperationsDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=snccostpoint;Initial Catalog=SNC_Operations;Persist Security Info=True;User ID=WT_Operations;Password=G&amp;E+KGxbg6WNjUu;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /><add name="Operations_Data_ModelContainer" connectionString="metadata=res://*/Operations_Data_Model.csdl|res://*/Operations_Data_Model.ssdl|res://*/Operations_Data_Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=snccostpoint;Initial Catalog=SNC_Operations;Persist Security Info=True;User ID=WT_Operations;Password=G&amp;E+KGxbg6WNjUu;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /><add name="SNC_OperationsEntities3" connectionString="metadata=res://*/OperationsModel.csdl|res://*/OperationsModel.ssdl|res://*/OperationsModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=snccostpoint;Initial Catalog=SNC_Operations;Persist Security Info=True;User ID=WT_Operations;Password=G&amp;E+KGxbg6WNjUu;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /></connectionStrings>
</configuration>

Open in new window

0
 
LVL 2

Author Comment

by:jimseiwert
ID: 33693924
That was the web.config from the wevservice project. I assume that is the one you wanted
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33698879
i have modified your config as below ....

under services - > service i have added a sample you will need to modify it as per your contract
<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="SilverlightFaultBehavior">
          <silverlightFaults/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add name="silverlightFaults"
          type="SilverlightFaultBehavior.SilverlightFaultBehavior,
        SilverlightFaultBehavior, Version=1.0.0.0,
        Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
     <services>
      <service behaviorConfiguration="WcfService.Service1Behavior" name="WcfService.SilverlightService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" contract="WcfService.ISilverlightService"
                behaviorConfiguration="SilverlightFaultBehavior"  >
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Open in new window

0
 
LVL 2

Author Comment

by:jimseiwert
ID: 33701217
when I added your part of the web.config. I get the error message saying that this part is a invalid child element

      <behavior name="SilverlightFaultBehavior">
          <silverlightFaults/>
        </behavior>


Any ideas why?
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33703105
can you post you web config file.... i cant find anything wrong with the one i posted other than the service contract names..which you would have changed while copying into your web config
0
 
LVL 2

Expert Comment

by:threesixteen
ID: 33810045
When I want to catch an exception in my silverlight clients from a webservice I'll put the exception handling in the webservice and in the event of an exception return the exception details to the client.

I then build logic into the client to handle that returned data and treat it as an exception..
0
 
LVL 2

Author Comment

by:jimseiwert
ID: 33885707
I tried that and  still can not find anything wrong. Can you post a sample app that can catch and display a simple "throw new exception("test message")

Thank you
0
 
LVL 2

Accepted Solution

by:
threesixteen earned 500 total points
ID: 33885895
Right - two things might be happening here, and without knowing what your webservice is doing in the backend it's tough to say for sure.  The first thing that's happening is that your webservice is truly returning no data, maybe because you're querying a database and nothing is getting found - or because an exception is happening in the web service.  The second thing is that an exception is happening in the web method, and since the exception is happening in the webservice there's really no exception in the silverlight client beyond no data getting returned from the web service.  

There's a couple of things you can do to troubleshoot from here:

1.) Call the webservice directly in a browser and try the method - depending on your IIS settings you'll either see a 500 or detailed exception info, in which case you can figure out what the problem is.  Best bet is to call the webservice on the server running the webservice itself, you should almost certainly get detailed info there.

2.) Wrap the web method you're using in a try/catch and in the event of an exception return some string either containing the exception data or something else that your silverlight client can deal with gracefully.

    <WebMethod()> _
    Public Function brokenFunction(ByVal inputString As String) As String
        Try
            'Some code that throws an exception belongs here
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question