0

The WCF Server is hosted on a Windows service. We are getting the following error Sporadically. I am sure that this is not an application error and is thrown within the WCF Framework when closing a connection. Anybody has seen this before and had luck fixing it please share. Any help is highly appreciated.

System.NullReferenceException: Object reference not set to an instance of an object.

Server stack trace: 
   at System.Threading.Overlapped.Free(NativeOverlapped* nativeOverlappedPtr)
   at System.ServiceModel.Channels.OverlappedContext.Free()
   at System.ServiceModel.Channels.OverlappedContext.FreeOrDefer()
   at System.ServiceModel.Channels.SocketConnection.Abort(TraceEventType traceEventType, String timeoutErrorString, TransferOperation transferOperation)
   at System.ServiceModel.Channels.SocketConnection.Abort()
   at System.ServiceModel.Channels.DelegatingConnection.Abort()
   at System.ServiceModel.Channels.ConnectionPool.AbortItem(IConnection item)
   at System.ServiceModel.Channels.IdlingCommunicationPool\`2.IdleTimeoutEndpointConnectionPool.AbortItem(TItem item)
   at System.ServiceModel.Channels.CommunicationPool\`2.EndpointConnectionPool.ReturnConnection(TItem connection, Boolean connectionIsStillGood, TimeSpan timeout)
   at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

Following is the excerpt from the client side.

    public DateTime GetLastReferenceDataRefreshedTime()
    {
        RfaReferenceServiceProxy serviceProxy = null;
        try
        {
            serviceProxy = SafeProxy();
            return serviceProxy.Channel.GetLastReferenceDataRefreshedTime();
        }
        catch (CommunicationException exception)
        {
            Log.Error("Retrying after communication exception:" + exception.Message);
            return SafeProxy().Channel.GetLastReferenceDataRefreshedTime();
        }
    }
Giri
  • 1
  • 1
  • Try to look into the inner exception to get infos about where the exception was thrown – Maximilian Ast Feb 27 '15 at 07:00
  • Are you running things async? It could be possible that something else is depending on the connection on a separate thread and that's why you're getting an unpredictable behavior. There could be a race condition and if you close connection before the other thread executes its routine, this happens. Look through your code and make sure that you're checking for `null`. Either way, we can't tell without the code. – B.K. Feb 27 '15 at 08:21
  • Can you post the code on the service when you're closing the connection please? – tom redfern Feb 27 '15 at 15:01
  • @TomRedfern, Is it possible to explicitly close a connection on the service. I believe it is automatically taken care by the Timouts. [Release Connection in server](http://stackoverflow.com/questions/11573077/release-connection-of-wcf-service-at-the-server-side). – Giri Mar 02 '15 at 03:33
  • @B.K. There are indeed async calls to the WCF services from the client. But on the client side I always make sure that I catch any Communication exception and safely clean the connection before creating a new proxy and calling the Method again. – Giri Mar 02 '15 at 03:35

1 Answers1

2

You are calling a method on the channel from within a catch block. This is not advisable, and I would be surprised if this is not the cause of your problem.

If SafeProxy().Channel is returning null then this would cause the observed behavior. It's impossible to know without seeing the contents of the SafeProxy method.

tom redfern
  • 30,562
  • 14
  • 91
  • 126