English 中文(简体)
我没有理解为什么在我的MVC项目中使用NoRM和Mongo。
原标题:I get this error that I don t understand why, using NoRM and Mongo in my MVC project

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

我不理解为什么发生这种情况以及如何处理。 我使用Ninject,我的申请是基于mvcstarter.codeplex.com/,我确实从我的数据库中删除一些用户或网页,而且这样做是毫无理由的(我可以发现)。

感谢任何帮助!

感谢很多!

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


}

发出呼吁的守则就象这样,我的——会议由我的主计长主持,使用Ninject,在我的全球范围内具有约束力:

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

EDITED:

我的错误细节:

Stack Trace : at System.Net.Sockets.TcpClient.GetStream() at Norm.Connection.GetStream() 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:inetpubwwwrootDubeLoiselleDomainStorageMongoDBMongoSession.cs:line 21 at Domain.Storage.MongoDB.MongoRepositoryBase1.Single(Expression1 expression) in C:inetpubwwwrootDubeLoiselleDomainStorageMongoDBMongoRepositoryBase.cs:line 26 at SPK.CMS.Domain.Repository.PageRepository.GetHomePage() in C:inetpubwwwrootDubeLoiselleSPK.CMS.DomainRepositoryPageRepository.cs:line 146 at Web.Controllers.PageController.Home() in C:inetpubwwwrootDubeLoiselleWebControllersPageController.cs:line 26 at Web.Controllers.PageController.RedirectTo(String url1, String url2, String url3) in C:inetpubwwwrootDubeLoiselleWebControllersPageController.cs:line 50 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)

www.un.org/Depts/DGACM/index_spanish.htm :

在我发现这一错误之前,我把这一错误trap为一:

无法将数据写给运输连接: 遥远的东道国强行关闭了现有的连接。

Stack Tra: at System.Net.Sockets.NetworkStream.Write(Byte[]缓冲区,Int32.50,Int32 volume) at Norm.Connection。 文献目录 Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext Controller) 背景,行动说明者行动说明,IDictionary 2 参数,系统。 Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.b__a() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilterfil), ActionExecutingContext preContext, Func 1sus) at System. Web.Mvc.ControllerActionInvoker. InvokeActionMethodWithFilters (ControllerContext Controller) 背景:IList1 过滤器, 行动说明者行动Descriptor, IDictionary 2参半 Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext ControllerContext, String actionName)

最佳回答

我为这一问题找到了解决办法,我在会议结束时反对我在“行动”中的控制者。

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

希望能帮助别人!

问题回答

之所以出现这种情况,是因为TcpClient与服务器脱节。

您应在最后时刻开放<代码>IMongo的链接,只查询NSQL数据储存和处理。 有一个连接库,使连接得以开通,并去除死亡链接,以降低使用<代码>Mongo.Create的费用。

如果你同时有多个客户,保持联系会给你带来问题。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签