c# - Handle persistent WCF client entering faulted state -


We've got a WCF service that we're consuming from the web app. The customer we are using was produced using the Visual Studio "Add Service Reference" option. Since this is a web app, and since there is a possibility of less session than the nature of the app, we have opted to create an example of a customer when a user logs in and keeps it for the life of the session, Then handle this by settling through the session.

This brings me to my question - we are trying to determine the best way to enter the customer's channel in a defective state. After searching around something, we have come up with it:

  if (client.State = CommunicationState.Faulted) {client = new Client (); } Try {Client.SomeMethod (); } Hold / Exception Exception {// logging or whatever we decide to throw; }   

This does not work due to the fact, at least in our case, even if the service is down, the client will be able to open Do not try to call using it, then at that point it then enters the defective state.

Then it leaves us to do something else. We came up with another option:

  try {client.SomeMethod (); } Hold {if (client.State == communication station.fault) {// We know that we are making a mistake, try client = new customer again; Try {Client.SomeMethod (); } Hold {throw; }} // handling other exceptions}   

But this smells obviously, we can avoid using it by using a new customer and disposing it for each call. It seems unnecessary, but if it is in the right way, then I think we will choose the option for it. So the customer is in a state of error and then what is the best way to behave gracefully on doing something about it? Should we really get a new customer for every call?

One more thing to keep in mind - the customer's immediate and all this is in the wrapper class for checking and handling clients if we intend to do so, this is transparent for the app - call There is no special code required to handle and handle exceptions.

To answer your question, you can manage the ChannelFactory property like this: < / P>

  client.ChannelFactory.Faulted + = New EventHeader (ChannelFactory_Faulted);   

You must allow any logging / cleanup that you need to do.

As a general recommendation, you should not leave the channel open for the duration, so make sure that you are closing the channel properly (except for the exception) after that you will end it

In addition, if possible, do not use Visual Studio Add Service Reference, or at least consider clearing that code / configuration I recommend that if you use a proxy implementation If you want to use To create, or use implementation. Since you mention a cover class, I suggest that you use a channel factor and handle it for your cleaning needs.

Comments