本文介绍了我在MVC项目中使用NoRM和Mongo时收到了一个我不明白为什么的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法访问已处置的对象.对象名称:"System.Net.Sockets.TcpClient".

Cannot access a disposed object.Object name: 'System.Net.Sockets.TcpClient'.

我不明白为什么会发生以及如何处理.我使用Ninject,我的应用程序基于mvcstarter.codeplex.com/,我的工作是从数据库中删除一些用户或页面,但这种情况无缘无故(我可以找到).

I don't understand why it happen and how to deal with it. I use Ninject, my application is based on mvcstarter.codeplex.com/ what I do is delete some user or pages from my database and it happen for no reason(that I can find).

任何帮助将不胜感激!

非常感谢!

*已编辑另外,过了一会儿它又恢复正常了,我可以从Mongo提取数据了……我的单元测试工作正常……

*Edited Also, after a while it get back to normal and I can fetch my data from Mongo... My unit tests work fine...

这是我的会话代码:

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();
    }


}

调用代码将是这样,我的_session使用Ninject传递到构造器中的控制器,绑定在我的global.cs中:

And the calling code would be something like this, my _session is pass to my Controller in the contrutor using Ninject, with the binding in 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();
    }

这是我的错误的详细信息:

Here's the detail of my error :

再次

在收到此错误之前,我先捕获了这个错误:

Before I get this error I trap this one :

无法将数据写入传输连接:远程主机强行关闭了现有连接.

Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.

堆栈跟踪:在System.Net.Sockets.NetworkStream.Write(Byte []缓冲区,Int32偏移量,Int32大小)在Norm.Connection.Write(Byte []字节,Int32起始,Int32大小)在Norm.Protocol System.Linq.Enumerable.SingleOrDefault [TSource](IEnumerable 1 source) at Norm.Linq.MongoQueryExecutor.Execute[T]() at Norm.Linq.MongoQueryProvider.ExecuteQuery[T](Expression expression) at Norm.Linq.MongoQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression) at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable 1源)处的.Messages.QueryMessage 2.Execute() at Norm.MongoQueryExecutor 3.d__0.MoveNext()位于Domain.Storage.MongoDB.MongoSession.Single [T](Expression .Single(Expression 1 expression) in C:\inetpub\wwwroot\DubeLoiselle\Domain\Storage\MongoDB\MongoRepositoryBase.cs:line 26 at SPK.CMS.Domain.Repository.PageRepository.GetHomePage() in C:\inetpub\wwwroot\DubeLoiselle\SPK.CMS.Domain\Repository\PageRepository.cs:line 146 at SPK.CMS.Domain.Repository.PageRepository.GetByUrl(String url1, String url2, String url3) in C:\inetpub\wwwroot\DubeLoiselle\SPK.CMS.Domain\Repository\PageRepository.cs:line 195 at Web.Controllers.PageController.RedirectTo(String url1, String url2, String url3) in C:\inetpub\wwwroot\DubeLoiselle\Web\Controllers\PageController.cs:line 52 at lambda_method(ExecutionScope , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2个参数)在System.Web.Mvc.ControllerActionActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.b__a() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func 1延续)在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList 1 filters, ActionDescriptor actionDescriptor, IDictionary 2个参数),位于System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,String actionName)

Stack Trace : at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size) at Norm.Connection.Write(Byte[] bytes, Int32 start, Int32 size) at Norm.Protocol.Messages.QueryMessage2.Execute() at Norm.MongoQueryExecutor3.d__0.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source) at Norm.Linq.MongoQueryExecutor.Execute[T]() at Norm.Linq.MongoQueryProvider.ExecuteQuery[T](Expression expression) at Norm.Linq.MongoQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression) at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable1 source) at Domain.Storage.MongoDB.MongoSession.Single[T](Expression1 expression) in C:\inetpub\wwwroot\DubeLoiselle\Domain\Storage\MongoDB\MongoSession.cs:line 21 at Domain.Storage.MongoDB.MongoRepositoryBase1.Single(Expression1 expression) in C:\inetpub\wwwroot\DubeLoiselle\Domain\Storage\MongoDB\MongoRepositoryBase.cs:line 26 at SPK.CMS.Domain.Repository.PageRepository.GetHomePage() in C:\inetpub\wwwroot\DubeLoiselle\SPK.CMS.Domain\Repository\PageRepository.cs:line 146 at SPK.CMS.Domain.Repository.PageRepository.GetByUrl(String url1, String url2, String url3) in C:\inetpub\wwwroot\DubeLoiselle\SPK.CMS.Domain\Repository\PageRepository.cs:line 195 at Web.Controllers.PageController.RedirectTo(String url1, String url2, String url3) in C:\inetpub\wwwroot\DubeLoiselle\Web\Controllers\PageController.cs:line 52 at lambda_method(ExecutionScope , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.b__a() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func1 continuation) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

推荐答案

我找到了解决此问题的方法,我在OnActionExecuted()中关闭了控制器中的Session对象

I found a solution for this problem, I close my Session object in my controller in the OnActionExecuted()

protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _session.Dispose();
        base.OnActionExecuted(filterContext);
    }

希望它可以帮助其他人!

Hope it can helps someone else!

这篇关于我在MVC项目中使用NoRM和Mongo时收到了一个我不明白为什么的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 06:10