OneTrueError and the WCF integration

OneTrueError has a complete WCF integration following the same pattern as WCF. It makes it a breeze to capture and analyze errors in WCF applications.

Installation

First of all you need to have an account at OneTrueError.com.

Start by installing the onetrueerror.wcf package. Then create a class with a AppInitialize() method. AppInitialize is the recommended way to initialize code in WCF applications. The class must be put in a folder named App_Code.

public class InitializeService
{
    public static void AppInitialize()
    {
        OneTrue.Configuration.Credentials("YourAppKey", "yourSharedSecret");
        OneTrue.Configuration.CatchWcfExceptions();
    }
}

That’s all you need to activate OneTrueError for automatic error handling. If you don’t get any reports uploaded to your account you can configure the library error handler as shown in this example.

Library activation

The above configuration will inject OneTrueError into the WCF pipeline, but you still need to tell OneTrueError which services you want to get automatic error handling for.

You can either do that by decorating your service class with our attribute:

[OneTrueErrorHandler]
public class OrderService : IOrderService
{
    // [...]
}

.. or by activating it through the configuration file:

<system.serviceModel>
  <extensions>
    <behaviorExtensions>
      <!-- the new behavior -->
      <add name="OneTrueErrorHandler" type="OneTrueError.Reporting.WCF.OneTrueErrorBehavior, OneTrueError.Reporting.WCF" />
    </behaviorExtensions>
  </extensions>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>

        <!-- and the activation -->
        <OneTrueErrorHandler />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

The end result is that our error identification is now attached to the response from WCF:

error_behavior.

By visiting the included link the user can track the error and leave feedback to you. You can also see the number of users which are tracking each incident (unique error) in your system.

We also honer the includeExceptionDetailInFaults configuration element. If you include it, we’ll attach exception information to the returned SOAP fault.

We do however have another configuration option: To include just the exception message (to give the user a hint about what went strong). To activate it, leave includeExceptionDetailInFaults to false and change the service attribute:

[OneTrueErrorHandler(IncludeExceptionMessage = true)]
public class OrderService : IOrderService
{
}

That’s pretty much the basics.

Customization

You might want to customize the error message or the fault message which is generated. That can be achieved by assigning your own factories to the library.

Custom error message

public class InitialiseService
{
    public static void AppInitialize()
    {
        OneTrue.Configuration.Credentials("YourAppKey", "yourSharedSecret");

        //assign the own factory
        OneTrue.Configuration.SetErrorMessageFactory(new CustomFaultMessageFactory());

        OneTrue.Configuration.CatchWcfExceptions();

    }
}

public class CustomFaultMessageFactory : IErrorMessageFactory
{
    public string Create(WcfErrorReporterContext context, string reportId)
    {
        var message = string.Format(@"You failed this application! 

You still have one chance left. Visit http://onetrueerror.com/feedback/{0}/ and make amends.", reportId);


        if (context.IncludeExceptionDetailInFaults)
            message += "rnrnError details: " + context.Exception;
        else if (context.IncludeExceptionMessageInFaults)
            message += "rnrnError reason: " + context.Exception.Message;

        return message;
    }
}

The result:

custom-message

Fault customization

SOAP errors are transferred as something which is called faults. These faults are basically wrapping the thrown exception to enable transport.

With OneTrueError you can customize the returned error by doing as follows:

public class InitializeService
{
    public static void AppInitialize()
    {
        OneTrue.Configuration.Credentials("YourAppKey", "yourSharedSecret");
        OneTrue.Configuration.SetFaultMessageFactory(myFaultFactory);
        OneTrue.Configuration.CatchWcfExceptions();
    }
}

And create your custom implementation of:

public interface IFaultMessageFactory
{
    Message Create(WcfErrorReporterContext context, MessageVersion version, string errorMessage);
}

The return value is a standard WCF message class.

Summary

All examples are available in our github account.