私は、同じマシン上にある WCF サービス (ServiceA) と Windows サービス (ServiceB) NetNamedPipeBinding
の間の通信に使用するアプリケーションに取り組んでいます。WSDualHttpBinding
ChannelFactory を作成し、続いて NetNamedPipe チャネルを作成する ServiceA にプロキシ クラスがあります。このチャネルはキャッシュされ、ServiceB とのすべての通信に使用されます。
NetNamedPipeBinding
基本的に、サービスを使用して電話をかける必要があるときはいつでも、次のChannelProxy
ようになります。
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;
}
}
これで、99% の確率ですべて正常に動作しますが、時折、ログの最後の行が表示されます - ManagerProxy's State == Opened
. このチェックの直後に、プロキシを使用してメソッドを呼び出そうとすると、次のエラーが発生します。
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]
State == Opened
パイプへの書き込みがすぐに失敗するのはなぜですか?
ProxyChannel を保持する方法にアーキテクチャ上の変更を加える必要はありますか?