wpf - WCF服务中的CommunicationObjectAbortedException和CommunicationObjectFaultedException

标签 wpf wcf exception service

我正在基于发布者订阅者模式的系统上工作。我有一个在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>

该系统可以长时间完美运行,没有任何问题。发生的事情是由于某种原因(我不确定),该服务有时会因以下异常而崩溃:

CommunicationObjectAbortedException或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方法中。我从所有测试中得出的原因之一是,当客户端异常关闭时会发生这种情况。例如,客户端进程已从任务管理器等中关闭。

但是,这个问题是一个巨大的痛苦,如果不解决此稳定性问题,我们将无法前进。有谁知道为什么通信对象出现故障并且服务崩溃了吗?

希望能对此得到一些积极的反馈。

谢谢。
乌默尔

最佳答案

经过研究,我自己解决了这个问题。问题是,每当我的客户端意外断开连接而没有正确取消订阅时,该服务就不会适本地照顾掉掉的客户端。因此,通信对象出现了故障。 Lee'sthis question的回答确实帮助我朝着正确的方向思考。经过进一步的研究,我发现this discussion在解决该问题方面非常有用。

关于wpf - WCF服务中的CommunicationObjectAbortedException和CommunicationObjectFaultedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14443390/

相关文章:

Canvas.clipPath 中的 Android UnsupportedOperationException

c# - 缩小 MahApps.Metro 中的 ProgressRing

wpf - Generic.xaml 有什么特别之处?

c# - 在 InkCanvas 中绘制的线条的发光效果

c# - 将 inkCanvas 中的图像保存为 png 或 jpeg 文件

wcf - 在没有 mex 绑定(bind)的情况下引用 WCF 服务

asp.net - 长时间运行的 http 进程 - 如何放入单独的进程?

c# - 尝试通过控制同时请求来解决可伸缩性问题

javascript - 异常在 JavaScript 中不起作用

jsf - 如何使用自定义异常处理程序处理 javax.el.PropertyNotFoundException