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?