tags:

views:

51

answers:

3

Hello,

I am hosting WCF service in console application(just temporary, I will move to service later. Using administrator account). After several hours of execution I get Critical Error from Console(HOST) and my service stop working. What is the best method to trace error? I have tried code below to log error but it seems code do not catch error. I think I need to handle error inside in WCF service not in host application. Any suggestions?

namespace ConsoleApplication1
{
    //test host project
    class Program
    {
        static void Main(string[] args)
        {                     

            using (var serviceHost = new ServiceHost(new ServicePdf()))
            {
                try
                {                    
                    serviceHost.Open();
                    Console.WriteLine("Service was succesfully hosted. Press [enter] to exit...");
                    Console.ReadLine();                    
                }
                catch (Exception ex)
                {                    
                    MethodToLogError(ex);
                    Console.WriteLine("Error occured while hosting service. Press [enter] to exit...");
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }
            }
        }
    }
}

Regards, Tomas

+1  A: 

You can catch any unhandled exception in your host (whether from the main thread or any other thread) by suscribing to the AppDomain.UnhandledException event.

static void Main(string[] args)
        {  
            //...
            AppDomain.CurrentDomain.UnhandledException += exceptionHandler;
            //...
}

static void exceptionHandler(object sender, UnhandledExceptionEventArgs args) 
{
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("An unhandledexception occurred. Time to die. " + e);
}

This way you get to log the exception. No way to gracefully recover from it this way, though.

Yann Schwartz
+1  A: 

You could try turning on the WCF trace and message logging.

http://mkdot.net/blogs/dejan/archive/2008/10/23/wcf-tracing-and-message-logging.aspx

Then use the Service trace View app to browse through the file.

MattC
A: 

If this happens after several hours, it sounds like a resource leak of some kind. Using Process Explorer, check the memory usage of the application; anything more than a couple of hundred megabytes usually implies a problem.

Tracing these is an art in itself; often a session with Windebug is required. It's usually an unreleased resource (in a WCF service, check your database code) or an issue with event handlers.

Jeremy McGee