Silverlight and Webservice Exceptions

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
LVL 2
jimseiwertAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
threesixteenConnect With a Mentor Commented:
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
 
Gautham JanardhanCommented:
you need to have fault hadling in your WCF service
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
jimseiwertAuthor Commented:
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
 
Gautham JanardhanCommented:
it should be Throw New FaultException(ex.Message)..if that doest work can you post you config file for service
0
 
jimseiwertAuthor Commented:
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
 
jimseiwertAuthor Commented:
That was the web.config from the wevservice project. I assume that is the one you wanted
0
 
Gautham JanardhanCommented:
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
 
jimseiwertAuthor Commented:
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
 
Gautham JanardhanCommented:
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
 
threesixteenCommented:
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
 
jimseiwertAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.