[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

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

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
Annette Wilson, MSIS
Asked:
Annette Wilson, MSIS
  • 2
  • 2
1 Solution
 
lenordisteCommented:
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
 
Annette Wilson, MSISAuthor Commented:
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
 
lenordisteCommented:
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
 
Annette Wilson, MSISAuthor Commented:
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
 
vipin_softCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now