2

I have am working on an application which uses NetNamedPipeBinding to communicate between a WSDualHttpBinding WCF service (ServiceA) and a Windows service (ServiceB) which are both on the same machine.

I have a proxy class in ServiceA which creates a ChannelFactory and, subsequently, the NetNamedPipe Channel. This channel is then cached and is used for all communications with ServiceB.

Basically, whenever I need to make a call using the NetNamedPipeBinding service, I get the ChannelProxy like this:

    private ManagerProxy _managerProxy;
    private ManagerProxy managerProxy
    {
        get
        {
            lock (lockObj)
            {
                if (_managerProxy == null)
                {
                    LogWarning("managerProxy", "_managerProxy == null. Creating proxy.");
                    _managerProxy = new ManagerProxy();
                }
                else if (_managerProxy != null && (_managerProxy.State == CommunicationState.Closed || _managerProxy.State == CommunicationState.Closing || _managerProxy.State == CommunicationState.Faulted))
                {
                    LogError("managerProxy", string.Format("_managerProxy.State == {0}. Attempting to renew proxy.", _managerProxy.State.ToString()), null, (int)ErrorReference.ProxyFaultedOrClosed);
                    try
                    {
                        _managerProxy.Close();
                    }
                    catch (Exception ex)
                    {
                        //Log exception;
                    }
                    _managerProxy = null;
                    _managerProxy = new ManagerProxy();
                }
                EventLog.WriteEntry("managerProxy", string.Format("ManagerProxy's State == {0}", _managerProxy.State), EventLogEntryType.Information);
            }
            return _managerProxy;
        }
    }

Now, 99% of the time, everything works fine, but occasionally, I'll get that last line in the log - ManagerProxy's State == Opened. Immediately after this check, I attempt to call a method using the proxy and I get the following error:

Action on the channel failed.
System.ServiceModel.CommunicationException: There was an error writing to the pipe: The pipe is being closed. (232, 0xe8).
    ---> System.IO.IOException: The write operation failed, see inner exception.
    ---> System.ServiceModel.CommunicationException: There was an error writing to the pipe: The pipe is being closed. (232, 0xe8).
    ---> System.IO.PipeException: There was an error writing to the pipe: The pipe is being closed. (232, 0xe8).
    at System.ServiceModel.Channels.PipeConnection.StartSyncWrite(Byte[] buffer, Int32 offset, Int32 size, Object& holder)
    at System.ServiceModel.Channels.PipeConnection.WriteHelper(Byte[] buffer, Int32 offset, Int32 size, Boolean immediate, TimeSpan timeout, Object& holder)
--- End of inner exception stack trace ---
    at System.ServiceModel.Channels.PipeConnection.WriteHelper(Byte[] buffer, Int32 offset, Int32 size, Boolean immediate, TimeSpan timeout, Object& holder)
    at System.ServiceModel.Channels.PipeConnection.Write(Byte[] buffer, Int32 offset, Int32 size, Boolean immediate, TimeSpan timeout)
    at System.ServiceModel.Channels.BufferedConnection.WriteNow(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, BufferManager bufferManager)
    at System.ServiceModel.Channels.BufferedConnection.Write(Byte[] buffer, Int32 offset, Int32 size, Boolean immediate, TimeSpan timeout)
    at System.ServiceModel.Channels.ConnectionStream.Write(Byte[] buffer, Int32 offset, Int32 count)
    at System.Net.Security.NegotiateStream.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
    at System.Net.Security.NegotiateStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
--- End of inner exception stack trace ---
    at System.Net.Security.NegotiateStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
    at System.Net.Security.NegotiateStream.Write(Byte[] buffer, Int32 offset, Int32 count)
    at System.ServiceModel.Channels.StreamConnection.Write(Byte[] buffer, Int32 offset, Int32 size, Boolean immediate, TimeSpan timeout)
--- End of inner exception stack trace ---

Server stack trace: 
    at System.ServiceModel.Channels.StreamConnection.Write(Byte[] buffer, Int32 offset, Int32 size, Boolean immediate, TimeSpan timeout)
    at System.ServiceModel.Channels.StreamConnection.Write(Byte[] buffer, Int32 offset, Int32 size, Boolean immediate, TimeSpan timeout, BufferManager bufferManager)
    at System.ServiceModel.Channels.FramingDuplexSessionChannel.OnSendCore(Message message, TimeSpan timeout)
    at System.ServiceModel.Channels.TransportDuplexSessionChannel.OnSend(Message message, TimeSpan timeout)
    at System.ServiceModel.Channels.OutputChannel.Send(Message message, TimeSpan timeout)
    at System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout)
    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)
    at [My code] [line]

Why do I get State == Opened and immediately afterwards failure to write to the pipe?
Is there maybe some architectural change I should make to the way I keep my ProxyChannel?

Guy Passy
  • 694
  • 1
  • 9
  • 32
  • 1
    Do you mean `NetNamedPipeBinding`? Anyway, there's the chance the server just happened to close it between the time you checked it and invoking it. –  Mar 09 '15 at 09:06
  • Did you check that you didn't excede the "maxReceivedMessageSize" if you're trying to get large amount of data ? – Emmanuel M. Mar 09 '15 at 09:15
  • @MickyDuncan yeah, that's what I mean. Edited - hopefully it's clearer now. – Guy Passy Mar 09 '15 at 09:45
  • @EmmanuelM. Well, I haven't really checked that... but so far with this project, my experience has been that if the max message size is exceeded, I get a message signifying that, and not a simple write fail to the pipe. – Guy Passy Mar 09 '15 at 09:46
  • @GuyPassy Thanks. Also I agree with you that WCF will report a different error for max message. –  Mar 09 '15 at 10:41
  • Did you ever figure this out? – Dude0001 Nov 17 '16 at 21:38
  • @Dude0001 IIRC, by the end of the project this error was no longer occurring but man... year and a half ago, I don't remember what exactly solved it - especially as it was an intermittent problem. There's even a chance that the problem never really went away and I simply implemented a retry layer between the proxy and the rest of the code. Unfortunately, I no longer have access to that code to check – Guy Passy Nov 20 '16 at 10:09

0 Answers0