0

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?

radu florescu
  • 4,315
  • 10
  • 60
  • 92
  • possible duplicate of [What is the best workaround for the WCF client `using` block issue?](http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue) – John Saunders Dec 28 '11 at 17:51
  • How is that not efficient? Also, I just identified your precise problem. Why do you think that's not it? – John Saunders Dec 28 '11 at 18:23
  • I don't want to open a channel every time i want to call the WCF. I want to use same channel every time and if it fails to start another one ore something. I just started working with WCF and I want some strategies regarding this matter – radu florescu Dec 28 '11 at 18:28
  • 1
    Suggestion: first, make your code work. Next, make your code work as fast as it needs to be. You are pre-optimizing, and may very well make your code break very efficiently. – John Saunders Dec 28 '11 at 18:29
  • 1
    While that statement is true in itself, I'm not sure it applies here. I wouldn't share your factory or channel faulting under pre-optimizing. – diggingforfire Dec 28 '11 at 18:40

2 Answers2

1

Do you use a ChannelFactory? If so, this answer shows an example of a ChannelFactoryManager that has worked pretty good for me.

Community
  • 1
  • 1
diggingforfire
  • 3,359
  • 1
  • 23
  • 33
0

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

radu florescu
  • 4,315
  • 10
  • 60
  • 92