You ServiceReference in ConsoleApplication1 is not updated and does not contain fault contract attribute. As you use .NET for the service and client it's better to share DataContract and ServiceContract between them.
For the test you can reference WcfService1 library in your ConsoleApplication1 application. When you do that, follow steps:
app.config in ConsoleApplication1:
<client> <endpoint address="net.tcp://localhost/WcfService1/Service1.svc" binding="netTcpBinding" contract="WcfService1.IService1" name="NetTcpBinding_IService2"> </endpoint> </client>
Client code:
try { var fact = new ChannelFactory<WcfService1.IService1>("NetTcpBinding_IService2"); var proxy = fact.CreateChannel(); var ves = proxy.GetData(1); Console.WriteLine(ves); } catch (FaultException<DivideByZeroException> exp) { Console.WriteLine(exp.Detail); }
Service code (I prefer to catch exception as much closer to code causing it as possible and then throws a concrete fault exception):
public string GetData(int value) { try { int i = 0; int y = 10/i; return string.Format("You entered: {0}", value); } catch (DivideByZeroException d) { throw new FaultException<DivideByZeroException>(d); } }
Your IErrorHandler:
public class WcfErrorHandler : IErrorHandler { public void ProvideFault(Exception error, MessageVersion version, ref Message fault) { if (error is FaultException) { //do nothing as it's already FaultException //you should do a transformation only in the case it's required } else { // Generate fault message manually including the exception as the fault detail MessageFault messageFault = MessageFault.CreateFault( new FaultCode("Sender"), new FaultReason(error.Message), error); fault = Message.CreateMessage(version, messageFault, null); } } public bool HandleError(Exception error) { //here you can log an exception return true; } }
P.S. It seems you have hosted your service incorrectly. To test it, I've referenced WcfService1 in ConsoleApplication1. And did following:
Added such lines into app.config of ConsoleApplication1:
<services> <service name="WcfService1.Service1" behaviorConfiguration="CalculatorServiceBehavior"> <endpoint address="" binding="netTcpBinding" contract="WcfService1.IService1" /> <!--<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" /> --> <host> <baseAddresses> <add baseAddress="net.tcp://localhost/WcfService1/Service1.svc"/> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavior"> <serviceDebug includeExceptionDetailInFaults="True" /> <errorHandler /> </behavior> </serviceBehaviors> </behaviors> <extensions> <behaviorExtensions> <add name="errorHandler" type="WcfService1.ErrorHandlerExtension, WcfService1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </behaviorExtensions> </extensions>
And before making a call to the service by client, I've started service:
private static void Main(string[] args) { var host = new ServiceHost(typeof(WcfService1.Service1)); host.Open(); ...