Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3876584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:23:55+00:00 2026-05-19T22:23:55+00:00

Cannot access a disposed object. Object name: ‘System.Net.Sockets.TcpClient’. I don’t understand why it happen

  • 0

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

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).

Any help would be appreciated!

Thanks a lot!

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

Here’s my code for my session :

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


}

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

EDITED :

Here’s the detail of my error :

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.MongoQueryExecutor
3.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](IQueryable
1
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.MongoRepositoryBase
1.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
Web.Controllers.PageController.Home()
in
C:\inetpub\wwwroot\DubeLoiselle\Web\Controllers\PageController.cs:line
26 at
Web.Controllers.PageController.RedirectTo(String
url1, String url2, String url3) in
C:\inetpub\wwwroot\DubeLoiselle\Web\Controllers\PageController.cs:line
50 at lambda_method(ExecutionScope ,
ControllerBase , Object[] ) at
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext, IDictionary
2
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, Func
1 continuation) at
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext
controllerContext, IList1 filters,
ActionDescriptor actionDescriptor,
IDictionary
2 parameters) at
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext, String actionName)

Edited again :

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.

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:23:55+00:00Added an answer on May 19, 2026 at 10:23 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting the following error: Cannot access a disposed object. Object name: 'AdoTransaction'.
I have an application that sporadically throws this exception: System.ObjectDisposedException: Cannot access a disposed
Hi could you help me with this error? Cannot access a disposed object. Object
I cannot seem to access the context object using a loop context is set:
We cannot access a private variable of a class from an object, which is
kt@rails-ubuntu:~/rails/ledger/releases/20080820144914/.git/hooks$ ls -al ls: cannot access post-commit: No such file or directory ls: cannot
I cannot understand the Oracle documentation. :-( Does anybody know how to fetch multiple
I cannot understand how this is possible. Please help!! I have an app with
In my clojure program I cannot access package scoped fields of the java class
I cannot figure out how to change the title bar icon (the icon in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.