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.
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:
.
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");
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:
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.