MongoDB的C#驱动报错Server instance 127.0.0.1:27017 is no longer connected的解决方案

2023-06-20,,

使用C#的MondoDB驱动,一直没问题。结果最近,MongoCursor的ToList方法,取列表,总是报错

Server instance 127.0.0.1:27017 is no longer connected

数据什么的也是正常。

最后终于发现导致问题出现的原因。虽然解决方法不是很完美,但是也还解决问题了。

如果有其它好的解决方法的同学可以和我交流,微信:Jeff520888

错误信息如下:

<html>
    <head>
        <title>Server instance 127.0.0.1:27017 is no longer connected.</title>
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Lucida Console";font-size: .9em}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
        </style>
    </head>

    <body bgcolor="white">

            <span><H1>“/”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1>

            <h2> <i>Server instance 127.0.0.1:27017 is no longer connected.</i> </h2></span>

            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

            <b> 说明: </b>执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

            <br><br>

            <b> 异常详细信息: </b>System.InvalidOperationException: Server instance 127.0.0.1:27017 is no longer connected.<br><br>

            <b>源错误:</b> <br><br>

            <table width=100% bgcolor="#ffffcc">
               <tr>
                  <td>
                      <code>

执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。</code>

                  </td>
               </tr>
            </table>

            <br>

            <b>堆栈跟踪:</b> <br><br>

            <table width=100% bgcolor="#ffffcc">
               <tr>
                  <td>
                      <code><pre>

[InvalidOperationException: Server instance 127.0.0.1:27017 is no longer connected.]
   MongoDB.Driver.MongoServerInstance.AcquireConnection() +140
   MongoDB.Driver.MongoServer.AcquireConnection(MongoServerInstance serverInstance) +259
   MongoDB.Driver.MongoCursorConnectionProvider.AcquireConnection() +56
   MongoDB.Driver.Operations.QueryOperation`1.KillCursor(IConnectionProvider connectionProvider, Int64 cursorId) +44
   MongoDB.Driver.Operations.<Execute>d__0.<>m__Finally6() +55
   MongoDB.Driver.Operations.<Execute>d__0.System.IDisposable.Dispose() +211
   MongoDB.Driver.Operations.<Execute>d__0.MoveNext() +598
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +406
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
   DAL.MongoHelper`1.Find(String collection, IMongoQuery query, String[] fields, String[] sortBys, Int32 skip, Int32 limit, Int64& totalNum) +934
   DAL.Service.CommonService.GetNodePageList(MongoPageCriteria criteria) +146
   DAL.Service.AppService.GetOrderListForPage(MongoPageCriteria criteria, Order ord) +5114
   Admin.BLL.AppManager.GetOrderListForPage(MongoPageCriteria criteria, Order order) +51
   Admin.View.OrderList.BindData() +807
   Admin.View.OrderList.OnLoad(EventArgs e) +516
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
</pre></code>

                  </td>
               </tr>
            </table>

            <br>

            <hr width=100% size=1 color=silver>

            <b>版本信息:</b> Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1016

            </font>

    </body>
</html>
<!--
[InvalidOperationException]: Server instance 127.0.0.1:27017 is no longer connected.
   在 MongoDB.Driver.MongoServerInstance.AcquireConnection()
   在 MongoDB.Driver.MongoServer.AcquireConnection(MongoServerInstance serverInstance)
   在 MongoDB.Driver.MongoCursor`1.MongoCursorConnectionProvider.AcquireConnection()
   在 MongoDB.Driver.Operations.QueryOperation`1.KillCursor(IConnectionProvider connectionProvider, Int64 cursorId)
   在 MongoDB.Driver.Operations.QueryOperation`1.<Execute>d__0.<>m__Finally6()
   在 MongoDB.Driver.Operations.QueryOperation`1.<Execute>d__0.System.IDisposable.Dispose()
   在 MongoDB.Driver.Operations.QueryOperation`1.<Execute>d__0.MoveNext()
   在 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   在 DAL.MongoHelper`1.Find(String collection, IMongoQuery query, String[] fields, String[] sortBys, Int32 skip, Int32 limit, Int64& totalNum)
   在 DAL.Service.CommonService.GetNodePageList(MongoPageCriteria criteria)
   在 DAL.Service.AppService.GetOrderListForPage(MongoPageCriteria criteria, Order ord)
   在 Admin.BLL.AppManager.GetOrderListForPage(MongoPageCriteria criteria, Order order)
   在 Admin.View.OrderList.BindData()
   在 Admin.View.OrderList.OnLoad(EventArgs e)
   在 System.Web.UI.Control.LoadRecursive()
   在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
[HttpUnhandledException]: 引发类型为“System.Web.HttpUnhandledException”的异常。
   在 System.Web.UI.Page.HandleError(Exception e)
   在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   在 System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   在 System.Web.UI.Page.ProcessRequest()
   在 System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   在 System.Web.UI.Page.ProcessRequest(HttpContext context)
   在 ASP.view_orderlist_aspx.ProcessRequest(HttpContext context) 位置 c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\7b58857c\5cb9b318\App_Web_0b0m0xbf.18.cs:行号 0
   在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
--><!--
此错误页可能包含敏感信息,因为 ASP.NET 通过 <customErrors mode="Off"/> 被配置为显示详细错误消息。请考虑在生产环境中使用 <customErrors mode="On"/> 或 <customErrors mode="RemoteOnly"/>。-->

MongoDB的C#驱动报错Server instance 127.0.0.1:27017 is no longer connected的解决方案的相关教程结束。

《MongoDB的C#驱动报错Server instance 127.0.0.1:27017 is no longer connected的解决方案.doc》

下载本文的Word格式文档,以方便收藏与打印。