Friday, April 29, 2011

How to make WCF service using nettcpbinding reconnect automaticlly?

I have an asynchronous WCF service using nettcpbinding. And I send a bunch of requests to it and get result by invoking EndDoWork() in my callback. If EndDonWork throw a exception once, all the invocation after that will throw exception said: communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

I think that's something close the connection because of the first exception. My question is: 1. what decide this behavior? If I use basicHttpBinding, the later invocation of EndDoWork work well. Is it related with keepAlive support? 2. Is there any property of configuration item I can set to ask service reconnect automatically?

From stackoverflow
  • If I remember correctly, you can avoid the channel faulting if you declare the Fault in the operation contract.

    For example:

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [FaultContract(typeof(MyDefinedFault))]
        void Operation();
    }
    

    As you have already declared MyDefinedFault in the Operation contract if you throw that from the service, the channel is not going to fault (unless of course you are using the System.ServiceModel.Description.ServiceDebugBehavior.IncludeExceptionDetailInFaults=true that may fault the channel anyways).

  • Where is the MyDefinedFault class. how to define this class.

    WillH : This is a class you create yourself. Can be very simple such as: [DataContract] public class MyDefinedFault { [DataMember] public string ExceptionString { get; set; } public MyDefinedFault(string exString) { ExceptionString = exString; } }

0 comments:

Post a Comment