views:

185

answers:

3

Sorry about my English

in WCF, Is there an event or method that catch unhandled exception? or i need to put try/catch in eny mathod

Thanks

A: 

You can catch all exceptions with:

try
{
   // code
   // put your wcf call in here
}
catch (Exception e)
{
    // catch stuff in here
}

It's not necessarily a good idea to just catch this though. What I tend to do is let your app crash in testing and then say if "EndPointNotFoundException" is thrown, I just catch this too. For example:

try
{
    // wcf stuff
}
catch(EndPointNotFoundException e)
{
    // deal with exception
}
catch(ServerTooBusyException e)
{
    // deal with exception
}
//etc...
ing0
+1  A: 

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.

Andrew Hare
It is very complicated, can you give a sample? and, need it to server-side. not client. Thanks
zvi
+1  A: 

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";
        }
    }
KenL
i need more than 1 wcf Service mathod. Is there anything similar to "onerror" event in global.asa, in ASP.NET that catch all unhandled exception?
zvi