Home > C#, WCF > Throwing a WCF FaultException to a Service Moniker based client

Throwing a WCF FaultException to a Service Moniker based client


Soap supports the notion of a Fault. This allows a web service to notify a client that an unexpected error has occurred during processing on the server. You should use this SOAP feature with care, because it could couple the client and the server together if it not carefully used. Yet in service oriented architectures, you want to decouple clients and servers as much as possible. Providing to much information about the error, like for example the server side stack trace, also introduces a security hole. But If Faults are used with care they provide an added value to web services.

WCF supports two types of Faults; generic faults implemented by FaultException and non generic faults implemented by FaultException<T>. The generic version essentially sends a string as fault payload back to the client, while the non generic version allows to specify a DataContract.

I recently discovered that the WCF service moniker supports the generic FaultException. In this article example, I will explain you how to implement it.

If you want more information about the WCF service moniker for COM based applications, please see my previous article:


Service Implementation

We are going to create a very simple WCF service, that accepts a string and that will throw the contents of the string as Fault back to the client.

The service interface:

namespace ServiceMonikerFaultsService
    public interface IFaultService
        void ThrowFault(string message);

The service implementation:

namespace ServiceMonikerFaultsService
    public class FaultService : IFaultService
        public void ThrowFault(string message)
            throw new FaultException(message);

The service configuration in the web.config:

    <service name="ServiceMonikerFaultsService.FaultService"
      <endpoint address=""
      <endpoint address="mex"
                contract="IMetadataExchange" />
      <behavior name="serviceBehavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />

You can now press F5 to launch your WCF service.

Client Implementation

We can now launch Excel to create a simple VBA client for our service, but of course you can call this service from every COM based program.

Our client code:

Sub ServicemonkercallMex()
    Dim service As Object
    Dim monString As String

    monString = "service4:mexaddress=http://localhost:49161/FaultService.svc/mex" & _
                ", address=http://localhost:49161/FaultService.svc" & _
                ", contract=IFaultService, contractNamespace=http://tempuri.org/" & _
                ", binding=WSHttpBinding_IFaultService, bindingNamespace=http://tempuri.org/"

    Set service = GetObject(monString)

    On Error GoTo HandleError:
    service.ThrowFault "A WCF Fault thrown to Excel"
    Exit Sub

    MsgBox Err.Description, vbCritical, "Error"
End Sub

Please do not forget to change the service address to the address of the WCF service on your local pc.

When we press F5 to launch the VBA code, we get the following response back from the WCF service:



FaultExceptions are the WCF mechanism to propagate exceptions from the service to the client. WCF supports both generic and non generic FaultExceptions. Generic FaultExceptions can only transmit a string value to the client, non generic FaultExceptions can transmit a DataContract. Unfortunately, the WCF Service Moniker only supports the generic version.

Categories: C#, WCF
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: