Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6000827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:44:59+00:00 2026-05-23T00:44:59+00:00

I have a self-hosted WCF service that is running as a Windows Service. For

  • 0

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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:45:00+00:00Added an answer on May 23, 2026 at 12:45 am

    Had the same problem, this solved it for me:

    Make your service implement this interface:

    public class MyService : System.ServiceModel.Description.IServiceBehavior
    

    Implement it like this:

        public void AddBindingParameters(System.ServiceModel.Description.ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<System.ServiceModel.Description.ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            return;
        }
    
        public void ApplyDispatchBehavior(System.ServiceModel.Description.ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher channel in serviceHostBase.ChannelDispatchers) { channel.ErrorHandlers.Add(new ErrorHandler()); }
        }
    
        public void Validate(System.ServiceModel.Description.ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            return;
        }
    

    And add this class:

    public class ErrorHandler : IErrorHandler
    {
       bool IErrorHandler.HandleError(Exception error)
        { return true; }
       void IErrorHandler.ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        { return;  }
    }
    

    Now set a breakpoint on HandleError.. it will show you the exception

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a self-hosted WCF service running as a windows service using the WebAPI
I have a self-hosted WCF service that is hosted by a desktop application. I
I have a self-hosted WCF web service running, and an Android client application. I
I have a wcf self-hosted service running as windows service, serving multiple win/web clients
I have a console app that runs a self hosted WCF Data Service. I
We have a WCF service that we recently switched from self-hosting to IIS-hosted. It
System Description: I have a WCF service (self hosted in a windows service) which
I have self hosted net tcp WCF service that exposes two methods and the
I have a self hosted WCF Rest service that I am using to simulate
I have a self hosted wcf service with a startup task that runs netsh

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.