CommunicationObjectAbortedException&CommunicationO

2019-08-07 16:56发布

我根据出版商订阅模式的系统上工作。 我有一个在WPF应用程序运行的WCF服务。 有其连接到服务众多客户。 该客户端还WPF。 我附上我下面的系统的代码片段:

服务:

[ServiceContract(Namespace = "http://AutoFXProfitsServer", SessionMode = SessionMode.Required, CallbackContract = typeof(ITradeMirrorClientContract))]
    public interface ITradeMirror
    {
        [OperationContract]
        string Subscribe(string userName, string password, int accountID);

        [OperationContract]
        bool Unsubscribe(string userName, string password, int accountID);

        [OperationContract]
        void PublishNewSignal(string signalInformation);
    }

    public interface ITradeMirrorClientContract
    {
        [OperationContract(IsOneWay = true)]
        void NewSignal(string signalInformation);
    }

    public class NewSignalEventArgs : EventArgs
    {
        public string SignalInformation;
    }
.
.
.
.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, AutomaticSessionShutdown = false)]
public class TradeMirrorService : DependencyObject, ITradeMirror
{
.
.
.
.

public string Subscribe(string userName, string password, int accountID)
    {
        try
        {
            if (AuthenticationSuccessful)
            {
                _callback = OperationContext.Current.GetCallbackChannel<ITradeMirrorClientContract>();
                _newSignalHandler = new NewSignalEventHandler(NewSignalHandler);
                NewSignalEvent -= _newSignalHandler;
                NewSignalEvent += _newSignalHandler;

                string suffixes = GetSuffixes();
                return suffixes;
            }
            else
            {
                return "FAILED";
            }
        }
        catch (Exception exception)
        {
            return "FAILED";
        }
    }

public bool Unsubscribe(string userName, string password, int accountID)
    {
        try
        {
            if (SearchHelper.UnAuthenticateUserCredentials(userName, password, accountID, _helper))
            {
                _callback = OperationContext.Current.GetCallbackChannel<ITradeMirrorClientContract>();
                _newSignalHandler = new NewSignalEventHandler(NewSignalHandler);
                NewSignalEvent -= _newSignalHandler;
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception exception)
        {
            return false;
        }
    }

public void PublishNewSignal(string signalInformation)
    {
        try
        {
            if (HeartBeatMessage())
            {

            }
            else
            {
                _systemOrderID++;

                signalInformation = TransformSignalInformation(signalInformation, _systemOrderID);
            }

            var e = new NewSignalEventArgs {SignalInformation = signalInformation};
            NewSignalEvent(this, e);
        }
        catch (Exception exception)
        {
        }
    }

而我的app.config:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding closeTimeout="23:59:59" openTimeout="23:59:59"     receiveTimeout="23:59:59" sendTimeout="23:59:59" transactionFlow="false" transferMode="Buffered" 
             transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="1000" maxBufferPoolSize="524288" maxBufferSize="65536" 
             maxConnections="1000" maxReceivedMessageSize="65536">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          <reliableSession ordered="true" inactivityTimeout="23:59:59" enabled="false"/>
          <security mode="None"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="false" httpGetUrl="http://95.138.188.232/autofxprofits/service"/>
         <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
  </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>    </configuration>

该系统完全适用于长时间没有任何问题。 什么情况是,由于某种原因(哪些我没有entirley确认)服务有时异常崩溃:

CommunicationObjectAbortedException OR CommunicationObjectFaultedException

System.ServiceModel.CommunicationObjectAbortedException: The communication object,  System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it has  been Aborted.

Server stack trace: 
   at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen()
   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 AutoFXProfitsServer.ITradeMirrorClientContract.NewSignal(String signalInformation)
   at AutoFXProfitsServer.TradeMirrorService.NewSignalHandler(Object sender, NewSignalEventArgs e) in D:\Work\Trade Mirror - Kumar\AutoFXToolsTradeMirror\AutoFXProfitsServer\Service.cs:line 232
   at AutoFXProfitsServer.TradeMirrorService.PublishNewSignal(String signalInformation) in D:\Work\Trade Mirror - Kumar\AutoFXToolsTradeMirror\AutoFXProfitsServer\Service.cs:line 171

要么,

System.ServiceModel.CommunicationObjectFaultedException: The communication object,     System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

Server stack trace: 
   at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen()
   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 AutoFXProfitsServer.ITradeMirrorClientContract.NewSignal(String signalInformation)
   at AutoFXProfitsServer.TradeMirrorService.NewSignalHandler(Object sender, NewSignalEventArgs e) in D:\Work\Trade Mirror - Kumar\AutoFXToolsTradeMirror\AutoFXProfitsServer\Service.cs:line 235
   at AutoFXProfitsServer.TradeMirrorService.NewSignalEventHandler.Invoke(Object sender, NewSignalEventArgs e)
   at AutoFXProfitsServer.TradeMirrorService.PublishNewSignal(String signalInformation) in D:\Work\Trade Mirror - Kumar\AutoFXToolsTradeMirror\AutoFXProfitsServer\Service.cs:line 174

只是重申,这些例外发生在PublishNewSignal方法。 我从所有的测试做出来的一个原因是,当客户端异常关闭发生这种情况。 例如,客户端进程从任务管理器等关闭

但这个问题是一个很大的痛苦,无需修复这个问题的稳定性我们不能移动forawrd。 没有人有任何想法,为什么通信对象变为故障和服务崩溃?

希望能得到这方面的一些积极的反馈。

谢谢。 乌默尔

Answer 1:

经过一番研究,我已经解决了这个问题我自己。 问题是,每当我的客户端意外断开,没有正确退订,服务没有正确采取删除的客户端的服务。 所以communciation对象成为故障。 李的回答这个问题,真的帮了我认为正确的方向。 一旦再有更多的研究,我发现这个讨论解决问题非常有用。



Answer 2:

基于你已经证明它是什么样子,你打开WCF通道(创建客户端),当你启动应用程序,然后直到应用程序关闭不关闭它。

有几个问题这种方法。 您正在运行到一个是网络或服务器上的任何中断都会导致通道无法使用。

做到这一点的方法是,你需要做一个WCF叫你打开通道,拨打电话,然后关闭该信道每次。

这种方法不仅提供了更强劲,以及更灵活的解决方案。



文章来源: CommunicationObjectAbortedException & CommunicationObjectFaultedException in WCF Service