Search code examples
wcfc#-4.0error-handlingwcf-client

How to handle WCF Service efficiently with error handling


I keep getting this error after I throw an error in WCF Service:

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

The error is thrown by service using
throw new FaultException<DatabaseFault>(new DatabaseFault(e.Message));

How can I handle this, in a way that doesn't need to restart both the client and the service each time?

I'm using WCF 4.0, C#.

Edit 1: I use net.tcp binding for the WCF service.

Edit 2: I want an efficient way to work with WCF. It is not related to what John S says.

Edit 3: My code works. But I think it's not running like it should(the way it was designed).

This is the console application which hosts my service :

using (ServiceHost host = new ServiceHost(typeof(AuctionService)))
        {
            Console.WriteLine("AuctionWCF.TestHost Started");
            host.AddServiceEndpoint(
                typeof(IAuctionService),
                new NetTcpBinding(),
                "net.tcp://localhost:9000/AuctionWcfEndPoint");
            host.Open();
            Console.WriteLine("AuctionWCF.TestHost listens for requests");

            Console.ReadLine();
        }

This is the client:

AuctionService = new ChannelFactory<IAuctionService>(
            new NetTcpBinding(),
            new EndpointAddress("net.tcp://localhost:9000/AuctionWcfEndPoint")).CreateChannel();

Am I doing something wrong?


Solution

  • This is a pretty good approach and this is what I wanted to discover:

    http://blog.tallan.com/2009/05/06/recover-from-a-wcf-service-fault-part-2-generic-serviceclientfactory-class/

    I took some of the good parts from diggingforfire answer too.

    Found anothe good answer that could be used as Best Practice here