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)
    	{
    	    //哒哒哒...
    	}
    }


    勉強心を持てば、生活は虚しくない!
  • 相关阅读:
    外贸邮箱哪个是安全邮箱?申请163电子邮箱可以吗?
    申请163电子邮箱,163邮箱格式是么样的?
    传统小游戏是否已过时?经典案例告诉你
    超越前作,H5游戏微创新是王道
    163邮箱哪个安全好用?常用的电子邮箱品牌有哪些?
    开通企业邮箱多少钱?企业邮箱怎么注册申请?
    如何保护电子邮箱安全?163邮箱安全吗?
    外贸邮箱怎么注册?集团公司企业邮箱哪个更适合?
    NBA战火重燃,H5游戏带你重拾青春回忆
    H5游戏定制选题知多少
  • 原文地址:https://www.cnblogs.com/beta2013/p/3377305.html
Copyright © 2011-2022 走看看