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
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

705 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