I have a self-hosted WCF service that is running as a Windows Service. For the tests, I'm running it as a console application.
I'd like to catch all unhandled exceptions that happen in the service and shutdown the host. I'd like to catch all non FaultExceptions that happen when producing a response, but also all exceptions that are thrown in "idle mode" - i.e. thrown from some worker threads.
Unfortunatelly, I can't handle the exceptions neither by IErrorHandler (not called), nor by AppDomain.CurrentDomain.UnhandledException (not raised).
Any clues?
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TestServiceBahavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="TestServiceBahavior" name="WcfErrorHandling.TestService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/TestService" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="" contract="WcfErrorHandling.ITestService" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>
The code:
using System;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
namespace WcfErrorHandling
{
[ServiceContract]
interface ITestService
{
[OperationContract]
string SayHello(string param);
}
public class TestService : ITestService
{
public string SayHello(string param)
{
if (param == "ae")
throw new ArgumentException("argument exception");
if (param == "fe")
throw new FaultException("fault exception");
return "hello";
}
}
public class TestHost : ServiceHost, IErrorHandler
{
public TestHost()
: base(typeof(TestService))
{
}
public void Start()
{
AppDomain.CurrentDomain.UnhandledException +=
(sender, ea) => UnhandledExceptionHandler(ea.ExceptionObject as Exception);
Open();
foreach (var channelDispatcher in ChannelDispatchers.OfType<ChannelDispatcher>())
channelDispatcher.ErrorHandlers.Add(this);
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// do nothing
}
public bool HandleError(Exception error)
{
if (!(error is FaultException))
UnhandledExceptionHandler(error);
return true;
}
private void UnhandledExceptionHandler(Exception ex)
{
Close();
Environment.Exit(1);
}
}
public class Program
{
public static void Main(string[] args)
{
var testHost = new TestHost();
testHost.Start();
Console.Out.WriteLine("Press any key to exit...");
Console.ReadKey();
testHost.Close();
}
}
}