Asp.net mvc 我得到了这个我不知道的错误';我不明白为什么在我的MVC项目中使用NoRM和Mongo

Asp.net mvc 我得到了这个我不知道的错误';我不明白为什么在我的MVC项目中使用NoRM和Mongo,asp.net-mvc,mongodb,norm,Asp.net Mvc,Mongodb,Norm,无法访问已释放的对象。 对象名称:“System.Net.Sockets.TcpClient” 我不明白为什么会这样,怎么处理。我使用Ninject,我的应用程序基于mvcstarter.codeplex.com/我所做的是从我的数据库中删除一些用户或页面,而这是毫无原因的(我可以找到) 任何帮助都将不胜感激 非常感谢 *编辑 另外,一段时间后,它恢复正常,我可以从Mongo获取我的数据。。。我的单元测试工作得很好 以下是我的会话代码: public class MongoSession : I

无法访问已释放的对象。 对象名称:“System.Net.Sockets.TcpClient”

我不明白为什么会这样,怎么处理。我使用Ninject,我的应用程序基于mvcstarter.codeplex.com/我所做的是从我的数据库中删除一些用户或页面,而这是毫无原因的(我可以找到)

任何帮助都将不胜感激

非常感谢

*编辑 另外,一段时间后,它恢复正常,我可以从Mongo获取我的数据。。。我的单元测试工作得很好

以下是我的会话代码:

public class MongoSession : ISession{

    private readonly Mongo _server;

    public MongoSession()
    {
        //this looks for a connection string in your Web.config - you can override this if you want
        _server = Mongo.Create("MongoDB");
    }

    public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class {
        return _server.GetCollection<T>().AsQueryable().Where(expression).SingleOrDefault();
    }

    public IQueryable<T> All<T>() where T : class {
        return _server.GetCollection<T>().AsQueryable();
    }

    /*public void Add<T>(T item) where T : class {
        _provider.DB.GetCollection<T>().Insert(item);
    }*/

    public void Save<T>(IEnumerable<T> items) where T : class {
        foreach (T item in items) {
            Save(item);
        }
    }

    public void Save<T>(T item) where T : class {
        var errors = DataAnnotationsValidationRunner.GetErrors(item);
        if (errors.Count() > 0)
        {
            throw new RulesException(errors);
        }
        _server.Database.GetCollection<T>().Save(item);
    }

    //this is just some sugar if you need it.
    /*public T MapReduce<T>(string map, string reduce) {
        T result = default(T);
        using (MapReduce mr = _provider.Server.CreateMapReduce()) {
            MapReduceResponse response =
                mr.Execute(new MapReduceOptions(typeof(T).Name) {
                                                                    Map = map,
                                                                    Reduce = reduce
                                                                });
            MongoCollection<MapReduceResult<T>> coll = response.GetCollection<MapReduceResult<T>>();
            MapReduceResult<T> r = coll.Find().FirstOrDefault();
            result = r.Value;
        }
        return result;
    }*/

    public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class
    {
        var items = All<T>().Where(expression);
        foreach (T item in items)
        {
            Delete(item);
        }
    }

    public void Delete<T>(T item) where T : class
    {
        _server.GetCollection<T>().Delete(item);
    }

    public void Drop<T>() where T : class
    {
        _server.Database.DropCollection(typeof(T).Name);

    }

    /*public void CommitChanges()
    {
        //mongo isn't transactional in this way... it's all firehosed
    }*/

    public void Dispose() {
        _server.Dispose();
    }


}
公共类MongoSession:ISession{
专用只读Mongo_服务器;
公共MongoSession()
{
//这将在Web.config中查找连接字符串-如果需要,可以覆盖它
_server=Mongo.Create(“MongoDB”);
}
public T Single(System.Linq.Expressions.Expression),其中T:class{
返回_server.GetCollection().AsQueryable().Where(表达式).SingleOrDefault();
}
public IQueryable All(),其中T:class{
返回_server.GetCollection().AsQueryable();
}
/*公共无效添加(T项),其中T:class{
_provider.DB.GetCollection().Insert(项);
}*/
公共作废保存(IEnumerable items),其中T:class{
foreach(项目中的T项目){
保存(项目);
}
}
公共作废保存(T项),其中T:class{
var errors=DataAnnotationsValidationRunner.GetErrors(项目);
if(errors.Count()>0)
{
抛出新规则异常(错误);
}
_server.Database.GetCollection().Save(项);
}
//如果你需要的话,这只是一些糖。
/*公共T映射还原(字符串映射,字符串还原){
T结果=默认值(T);
使用(MapReduce mr=\u provider.Server.CreateMapReduce()){
地图还原反应=
mr.Execute(新MapReduceOptions(typeof(T).Name){
Map=Map,
减少
});
MongoCollection coll=response.GetCollection();
MapReduceResult r=coll.Find().FirstOrDefault();
结果=r值;
}
返回结果;
}*/
public void Delete(System.Linq.Expressions.Expression),其中T:class
{
var items=All()。其中(表达式);
foreach(项目中的T项目)
{
删除(项目);
}
}
公共作废删除(T项),其中T:class
{
_server.GetCollection().Delete(项);
}
公共void Drop(),其中T:class
{
_server.Database.DropCollection(typeof(T).Name);
}
/*公共无效佣金()
{
//mongo不是以这种方式进行交易的……它完全是火爆的
}*/
公共空间处置(){
_Dispose();
}
}
调用代码如下所示,my_会话使用Ninject传递给contrutor中的控制器,绑定在my global.cs中:

Bind<ISession>().To<MongoSession>().InSingletonScope();



public virtual ActionResult Liens()
    {
        var shortcutLionks = _session.All<ShortcutLinks>().ToList();
        ViewData.Model = shortcutLionks.Count > 0 ? shortcutLionks[0] : new ShortcutLinks();
        return View();
    }
Bind().To().InSingletonScope();
公共虚拟操作结果留置权()
{
var shortcutLionks=_session.All().ToList();
ViewData.Model=shortcutLionks.Count>0?shortcutLionks[0]:新的ShortcutLinks();
返回视图();
}
编辑:

以下是我的错误的详细信息:

堆栈跟踪:在 System.Net.Sockets.TcpClient.GetStream() at Norm.Connection.GetStream()位于 Norm.Connection.Write(字节[]字节, Int32开始,Int32大小)在 Norm.Protocol.Messages.QueryMessage
2.Execute()
在
Norm.MongoQueryExecutor
3.d_uu0.MoveNext() 在 System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable
1
来源)在
Norm.Linq.MongoQueryExecutor.Execute[T]()
在
Norm.Linq.MongoQueryProvider.ExecuteQuery[T](表达式
表达式)在
Norm.Linq.MongoQueryProvider.System.Linq.IQueryProvider.Execute[S](表达式
表达式)在
System.Linq.Queryable.SingleOrDefault[TSource](IQueryable
1 来源)在 Domain.Storage.MongoDB.MongoSession.Single[T](表达式
1
表达)在
C:\inetpub\wwwroot\DubeLoiselle\Domain\Storage\MongoDB\MongoSession.cs:line
21时
Domain.Storage.MongoDB.MongoRepositoryBase
1.Single(表达式
1
表达)在
C:\inetpub\wwwroot\DubeLoiselle\Domain\Storage\MongoDB\MongoRepositoryBase.cs:line
26在
SPK.CMS.Domain.Repository.PageRepository.GetHomePage()
在里面
C:\inetpub\wwwroot\DubeLoiselle\SPK.CMS.Domain\Repository\PageRepository.cs:line
146 at
Web.Controllers.PageController.Home()
在里面
C:\inetpub\wwwroot\DubeLoiselle\Web\Controllers\PageController.cs:line
26在
Web.Controllers.PageController.RedirectTo(字符串
中的url1、url2、url3)字符串
C:\inetpub\wwwroot\DubeLoiselle\Web\Controllers\PageController.cs:line
lambda_法(执行范围,
ControllerBase,对象[])位于
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext,IDictionary
2 参数)在 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary
2
参数)在
System.Web.Mvc.ControllerActionInvoker.c_uudisplayClassD.b_uua()
在
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter
过滤器,ActionExecutingContext
预文本,Func
1续) System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext,IList
1过滤器,
ActionDescriptor ActionDescriptor,
IDictionary
2个参数) System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,字符串actionName)

再次编辑:

在我得到这个错误之前,我捕获了这个错误
protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _session.Dispose();
        base.OnActionExecuted(filterContext);
    }