zvi
zvi

Reputation: 53

Catch unhandled exception in WCF

In WCF, is there an event or method that catches unhandled exceptions, or do I need to put a try/catch in any method?

Upvotes: 5

Views: 4094

Answers (2)

KenL
KenL

Reputation: 875

You should do Inner and outer TRY/Catch Blocks.

So the first method starts with Try

Then if anything is thrown within another method it defaults to your generic catch in the method that is in the exposed method to return a value to the client.

I always use logging in my catch blocks to tell an admin what went wrong but I aways have the outer catch return a value of something like "Please Except our Appogies the WCF.Blah service has failed. Please review the server logs for complete details"

This way you have error handling, logging and nice messages to your clients..

public class Service1 : IService1
    {
    public string GetData(int value)
    {
        try
        { 
            return somemethod(value);
        }
        catch(Exception ex)
        {
            LoggingHelper.Log(ex);
            return "Please Except our Appogies the WCF.Blah service has failed. Please review the server logs for complete details";
        }
    }

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351646

Yes, create a class that implements the IErrorHandler interface:

Allows an implementer to control the fault message returned to the caller and optionally perform custom error processing such as logging.

Upvotes: 7

Related Questions