Solved

Silverlight and Webservice Exceptions

Posted on 2010-09-14
12
799 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
Comment Utility
you need to have fault hadling in your WCF service
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
0
 
LVL 2

Author Comment

by:jimseiwert
Comment Utility
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
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
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
Comment Utility
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
Comment Utility
That was the web.config from the wevservice project. I assume that is the one you wanted
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now