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

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!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 69
Problem with SqlConnection 5 123
Saveas need to save a copy 16 14
Open a word document 23 19
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…
As a financial services provider, your business is impacted by two of the strictest federal regulations on record: the Sarbanes-Oxley Act and the Gramm-Leach-Bliley Act. Correctly implementing faxing into your organization to provide secure, real-ti…
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…
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now