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
401 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
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…
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…
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…

864 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

20 Experts available now in Live!

Get 1:1 Help Now