Click here to Skip to main content
Click here to Skip to main content
Technical Blog

OneTrueError and the WCF integration

, 4 Aug 2014 LGPL3
Rate this:
Please Sign up or sign in to vote.
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 … Continue reading →

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 += "\r\n\r\nError details: " + context.Exception;
        else if (context.IncludeExceptionMessageInFaults)
            message += "\r\n\r\nError 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.

Result

Summary

All examples are available in our github account.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.
 
blog | twitter
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 4 Aug 2014
Article Copyright 2014 by jgauffin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid