Solved

How do I catch a WCF Service Error and return a nicer message in a window to user?

Posted on 2011-03-08
5
402 Views
Last Modified: 2013-12-06
I have been reading through a number of examples on fault handling on WCF Web Service...  Still not very clear to me.  

I am currently demonstrating the application as User Acceptance Testing kick-off and the Silverlight 4 application runs perfectly from my teams desktops.

When I'm showing the application through webex, I get an error, then when I retry, the data comes in.

The user may experience this at times... I just want to be able to:

1.  Catch the error
2.  Display a nice message, saying "Error contacting service, please try again".

How can I do this.   Visual Basic
0
Comment
Question by:Annette Wilson, MSIS
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
lenordiste earned 500 total points
ID: 35074468
i usually handle WCF errors like this:
try
{
  yourWcfProxy.DoSomething();
   yourWcfProxy.Close();
}
catch (FaultException ex)
{
  // handle any faults returned by your WCF service
   yourWcfProxy.Abort();
}
catch (CommunicationException ex)
{
  // handle any errors encountered while trying to contact your WCF service
  yourWcfProxy.Abort();
}

Open in new window


The important thing is to avoid the "using" construct as it is bad practice for WCF.

here is some useful info on error handling in WCF: http://blogs.msdn.com/b/pedram/archive/2008/01/25/wcf-error-handling-and-some-best-practices.aspx
0
 

Author Comment

by:Annette Wilson, MSIS
ID: 35075652
Do you mean this?  Please forgive my ignorance.
Try
 AddHandler ContractServ.SPPerformCompleted, AddressOf SPPerformCompleted
        ContractServ.SPPerformAsync(txtPerfNum.Text)
	ContractServ.SPPerformCompleted.Close()
Catch ex As FaultException
	' handle any faults returned by your WCF service
	ContractServ.SPPerformCompleted.Abort()
Catch ex As CommunicationException
	' handle any errors encountered while trying to contact your WCF service
	ContractServ.SPPerformCompleted.Abort()
End Try

Open in new window

0
 
LVL 11

Expert Comment

by:lenordiste
ID: 35087269
Are you calling your WCF service asynchronously on purpose? Handling exceptions for asynchronous calls is a different beast.

For synchronous:
Try
        ContractServ.DoSomething() 'where DoSomething is a method you implemented
	ContractServ..Close()
Catch ex As FaultException
	' handle any faults returned by your WCF service
	ContractServ.Abort()
Catch ex As CommunicationException
	' handle any errors encountered while trying to contact your WCF service
	ContractServ.Abort()
End Try

Open in new window

0
 

Author Comment

by:Annette Wilson, MSIS
ID: 35238570
Leordiste,

Thank you for responding.  I'm sorry I'm taking so long to get back.  I will get back to this in two more days.  I am symutaneously releasing two software projects.
0
 
LVL 1

Expert Comment

by:vipin_soft
ID: 37548561
Please check these links,

http://forums.silverlight.net/p/98385/273886.aspx
http://mark.mymonster.nl/2011/02/10/make-use-of-wcf-faultcontracts-in-silverlight-clients


Or alternate solution is you should return your own class from WCFService function with two property(Error,Result) and on the client side(Silverlight) you should check Error property and in this way you can easily trap your exception.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Outsource Your Fax Infrastructure to the Cloud (And come out looking like an IT Hero!) Relative to the many demands on today’s IT teams, spending capital, time and resources to maintain physical fax servers and infrastructure is not a high priority.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

810 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