WCF返回长List出错:"wcf 基础连接已经关闭: 连接被意外关闭"

时间:2022-08-28 17:28:45
返回长对象列表,开始出现问题是
已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。
配置文件修改MaxReceivedMessageSize后解决。
但是当返回列表中对象超过一定数量(2000)后,出现错误:
未处理 System.ServiceModel.CommunicationException
  Message="基础连接已经关闭: 连接被意外关闭。"
  Source="mscorlib"
  StackTrace:
    Server stack trace: 
       在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
       在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       在 System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       在 System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
       在 System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
       在 System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
       在 System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message message, TimeSpan timeout)
       在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
       在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
       在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    Exception rethrown at [0]: 
       在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       在 TestService.ShopServiceReference.IShopService.GetMembers()
       在 TestService.ShopServiceReference.ShopServiceClient.GetMembers() 位置 E:\code\ShopService\TestService\Service References\ShopServiceReference\Reference.cs:行号 681
       在 TestService.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\code\ShopService\TestService\Form1.cs:行号 24
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 TestService.Program.Main() 位置 E:\code\ShopService\TestService\Program.cs:行号 18
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Net.WebException
       Message="基础连接已经关闭: 连接被意外关闭。"
       Source="System"
       StackTrace:
            在 System.Net.HttpWebRequest.GetResponse()
            在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       InnerException: 


怎么解决

3 个解决方案

#1


<binding name="basicHttpBinding" maxBufferSize="2048000" maxBufferPoolSize="1048576" maxReceivedMessageSize="2048000">
...
</binding>

把上面的值去掉,或者改大些。在web.config或者app.config里面

#2


{System.ServiceModel.CommunicationException}
"   位于 System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)\r\n   位于 System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)\r\n   位于 System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)\r\n   位于 System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)\r\n   位于 YautaSoft.SmartEMS.Silverlight.SmartEMSService.ServiceClient.ServiceClientChannel.EndGetAllUsers(IAsyncResult result)\r\n   位于 YautaSoft.SmartEMS.Silverlight.SmartEMSService.ServiceClient.YautaSoft.SmartEMS.Silverlight.SmartEMSService.Service.EndGetAllUsers(IAsyncResult result)\r\n   位于 YautaSoft.SmartEMS.Silverlight.SmartEMSService.ServiceClient.OnEndGetAllUsers(IAsyncResult result)\r\n   位于 System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)"

我觉得有可能是List的问题,但是奇怪的是我这里一会儿可以一会儿不可以,把我都搞晕了

#3


我也遇到过,一般情况下士数据访问的错误

#1


<binding name="basicHttpBinding" maxBufferSize="2048000" maxBufferPoolSize="1048576" maxReceivedMessageSize="2048000">
...
</binding>

把上面的值去掉,或者改大些。在web.config或者app.config里面

#2


{System.ServiceModel.CommunicationException}
"   位于 System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)\r\n   位于 System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)\r\n   位于 System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)\r\n   位于 System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)\r\n   位于 YautaSoft.SmartEMS.Silverlight.SmartEMSService.ServiceClient.ServiceClientChannel.EndGetAllUsers(IAsyncResult result)\r\n   位于 YautaSoft.SmartEMS.Silverlight.SmartEMSService.ServiceClient.YautaSoft.SmartEMS.Silverlight.SmartEMSService.Service.EndGetAllUsers(IAsyncResult result)\r\n   位于 YautaSoft.SmartEMS.Silverlight.SmartEMSService.ServiceClient.OnEndGetAllUsers(IAsyncResult result)\r\n   位于 System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)"

我觉得有可能是List的问题,但是奇怪的是我这里一会儿可以一会儿不可以,把我都搞晕了

#3


我也遇到过,一般情况下士数据访问的错误