Solved

Silverlight and Webservice Exceptions

Posted on 2010-09-14
12
811 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

19 Experts available now in Live!

Get 1:1 Help Now