Jon-Eric
Jon-Eric

Reputation: 17295

Log exceptions thrown by WCF service constructor?

Is there any way to capture and log exceptions thrown from a WCF service's constructor?

Creating a custom IEndpointBehavior with a custom IErrorHandler seems to catch all exceptions except those thrown during service construction. (Please correct me if I'm wrong.)

I can see from the HTTP response that this situation eventually generates a System.ServiceModel.ServiceActivationException, but it would be helpful if I could log out the details from the original exception.

Upvotes: 2

Views: 697

Answers (2)

John Meyer
John Meyer

Reputation: 2356

You will need to create a class to log the exception, initialize it in the constructor and call it when an exception is caught. Below is a very simple example:

public class MyWcfService : IMyWcfService
{
    private readonly IExceptionLogger_ exceptionLogger;

    public MyWcfService()
    {
        // Initialize dependencies
        _exceptionLogger = new ExceptionLogger();

        try
        {           
            // Code here that throws an exception
        }
        catch (Exception ex)
        {
            _exceptionLogger.LogException(ex);
        }

    } // end constructor

    // Other methods here

} // end class

Upvotes: 0

Mike Goodwin
Mike Goodwin

Reputation: 8880

You can use WCF tracing

http://msdn.microsoft.com/en-us/library/ms733025.aspx

You can turn it on in the service configuration so no need to instrument your code.

Upvotes: 2

Related Questions