zoukankan      html  css  js  c++  java
  • “WCF服务连接丢失”问题及解决

    一  现象

    在“输出-调试”窗口外输出如下异常信息

    System.ServiceModel.CommunicationException: 套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的。本地套接字超时是“10675199.02:48:05.4775807”。 ---> System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接。
       --- 内部异常堆栈跟踪的结尾 ---
       在 System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
       在 System.ServiceModel.Channels.FramingDuplexSessionChannel.EndTryReceive(IAsyncResult result, Message& message)
       在 System.ServiceModel.Dispatcher.DuplexChannelBinder.EndTryReceive(IAsyncResult result, RequestContext& requestContext)
       在 System.ServiceModel.Dispatcher.ErrorHandlingReceiver.EndTryReceive(IAsyncResult result, RequestContext& requestContext)




    二  服务器端异常捕捉方式

    服务器端的IContextChannel(OperationContext.Current.Channel).Faulted事件




    三  原因分析及解决

    原因1:消息包长度受限

    解决:加长长度,参考http://www.cnblogs.com/snowsky/archive/2010/12/22/1913601.html

    示例:

    服务端配置:

    <basicHttpBinding>
    	<binding name="basicBinding" maxReceivedMessageSize="2147483647"></binding>
    </basicHttpBinding>
    <behavior name="bcf">
    	<serviceMetadata httpGetEnabled="true" />
    	<serviceDebug includeExceptionDetailInFaults="false" />
    	<dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
    </serviceBehaviors>


    客户端配置:

    <behaviors>
    	<endpointBehaviors>
    		<behavior name="bhc">
    			<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    		</behavior>
    	</endpointBehaviors>
    </behaviors>
    <basicHttpBinding>
    	<binding name="basicBinding" maxReceivedMessageSize="2147483647"></binding>
    </basicHttpBinding>
    




    原因2、客户端创建使用完服务代理后未回收此资源

    解决:回收

    示例:

    using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice"))
    {
    	ICalculator proxy = channelFactory.CreateChannel();
    	using (proxy as IDisposable)
    	{
    	    //哒哒哒...
    	}
    }


    勉強心を持てば、生活は虚しくない!
  • 相关阅读:
    【深入理解JAVA虚拟机】第一部分.走进Java
    【设计模式最终总结】桥接模式 VS 外观模式
    【设计模式最终总结】组合模式:树形结构的处理
    【设计模式最终总结】桥接模式(接口模式):处理多维度变化
    【设计模式最终总结】适配器模式
    深入编解码:ASCII,UNICODE,UTF8等
    好妈妈【第四章】培养良好的学习习惯
    【设计模式最终总结】建造者模式:复杂对象的组装与创建
    上火流鼻血
    Spring Data Jpa 学习笔记
  • 原文地址:https://www.cnblogs.com/beta2013/p/3377305.html
Copyright © 2011-2022 走看看