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

  • SEARCH
  • Home
  • 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 4625528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:17:08+00:00 2026-05-22T03:17:08+00:00

This is my service class: public class MyService { private readonly MyDataContext _db; public

  • 0

This is my service class:

public class MyService
{
  private readonly MyDataContext _db;

  public MyService()
  {
    _db = new MyDataContext(GetConnectionString());
#if DEBUG
    _db.Log = Console.Error;
#endif
  }

  public void Get(int id)
  {
    return from item in _db.Items
           where item.Id == id
           select item;
  }
}

This is my test class

[TestClass]
public class MyServiceTest
{
  private MyService _service = new MyService();

  [TestMethod]
  public void CanGetSomething()
  {
     var something = _service.Get(1).ToList();
     // Asserts
  }

  [TestMethod]
  public void CanGetSomethingElse()
  {
    var somethingElse = _service.Get(2).ToList();
    // Commented out everything else.
  }
}

Running CanGetSomething and CanGetSomethingElse separately using the ReSharper 5 test runner works fine, both tests pass. However, trying to run the tests in succession by running the entire class causes the first method to pass and the second method to throw the exception

System.ObjectDisposedException: Cannot write to a closed TextWriter.

It doesn’t seem to matter what method I call in the second test, anything that calls _db will cause the error. Commenting out _db.Log = Console.Error gets rid of the exception, and it will work fine.

I’d like to be able to log the error and run an entire class of tests at once, but I can’t figure out why it is behaving like this.

Any ideas?

  • 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-22T03:17:09+00:00Added an answer on May 22, 2026 at 3:17 am

    Dont share members of your test class! Not knowing resharper, but VS 2010 UnitTesting is really kean on cleaning up after running a test. My assumption for what happens here is when constructing your TestClass, your context is being initialized. And after running a single test. Resharper cleans up the resources, therefore also disposing your context.

    Use a TestInitialize to create a new instance of your dataContext, therefore isolating your tests from one another

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

Sidebar

Related Questions

I have a service class : public class MyService : IService { private IRepoA
I currently have service classes that look something like this public class UserService :
Given I have a Spring bean configured as @Service(myService) public class DefaultService extends MyService
I have a Windows service built upon ATL 7's CAtlServiceModuleT class. This service serves
I have a service called myService.svc. This service is exposed using a webHttpBinding binding.
I have created an web service I need to publish this service. I need
So I need to access this service from my rails app. I'm using soap4r
I am writing a windows service. This service runs another process I've developed through
I have a web service implemented in WCF. This service is only going to
I have an echo web service running on lets say http://localhost:8080/axis2/services/Service1 . This service

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.