Click here to Skip to main content
15,888,113 members
Articles / General Programming / Exceptions

OneTrueError and the WCF Integration

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Aug 2014LGPL32 min read 9.7K   2  
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 <c>AppInitialize() method. AppInitialize is the recommended way to initialize code in WCF applications. The class must be put in a folder named App_Code.

C#
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:

C#
[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:

C#
[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

C#
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:

C#
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:

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

The return value is a standard WCF message class.

Result

Image 3

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)


Written By
Founder 1TCompany AB
Sweden Sweden

Comments and Discussions

 
-- There are no messages in this forum --